@gidw/mac-address-util
Advanced tools
Comparing version 1.0.1 to 1.0.2
117
index.js
'use strict' | ||
module.exports = { | ||
exports.convertToMac = convertToMac | ||
exports.convertToNumber = convertToNumber | ||
/** | ||
* Convert a numeric MAC address to a standard MAC address notation | ||
* | ||
* @param {number} macNum | ||
* @returns {string} | ||
*/ | ||
convertToMac: function convertToMac (macNum) { | ||
var macStr, macStrLength, macStrLengthDiff, completeMacStr | ||
var i, padding | ||
var result = '' | ||
/** | ||
* Convert a numeric MAC address to a standard MAC address notation | ||
* | ||
* @param {number} macNum | ||
* @returns {string} | ||
*/ | ||
function convertToMac (macNum) { | ||
var result, macStr, macStrLength, macStrLengthDiff, completeMacStr, padding, i | ||
result = '' | ||
if (typeof macNum === 'number' && | ||
macNum >= 0 && | ||
macNum <= 281474976710655) { | ||
macStr = macNum.toString(16) | ||
macStrLength = macStr.length | ||
macStrLengthDiff = 12 - macStrLength | ||
if (typeof macNum === 'number' && | ||
macNum >= 0 && | ||
macNum <= 281474976710655) { | ||
macStr = macNum.toString(16) | ||
macStrLength = macStr.length | ||
macStrLengthDiff = 12 - macStrLength | ||
if (macStrLengthDiff === 0) { | ||
completeMacStr = macStr | ||
} else if (macStrLengthDiff > 0 && macStrLengthDiff <= 12) { | ||
padding = '' | ||
for (i = 0; i < macStrLengthDiff; i++) { | ||
padding += '0' | ||
} | ||
completeMacStr = padding + macStr | ||
if (macStrLengthDiff === 0) { | ||
completeMacStr = macStr | ||
} else if (macStrLengthDiff > 0 && macStrLengthDiff <= 12) { | ||
padding = '' | ||
for (i = 0; i < macStrLengthDiff; i++) { | ||
padding += '0' | ||
} | ||
completeMacStr = padding + macStr | ||
} | ||
if (typeof completeMacStr === 'string' && | ||
completeMacStr.length === 12) { | ||
for (i = 0; i < 6; i++) { | ||
result += completeMacStr[2 * i] + | ||
completeMacStr[(2 * i) + 1] + ':' | ||
} | ||
result = result.substring(0, 17) | ||
if (typeof completeMacStr === 'string' && | ||
completeMacStr.length === 12) { | ||
for (i = 0; i < 6; i++) { | ||
result += completeMacStr[2 * i] + | ||
completeMacStr[(2 * i) + 1] + ':' | ||
} | ||
result = result.substring(0, 17) | ||
} | ||
} | ||
return result | ||
}, | ||
return result | ||
} | ||
/** | ||
* Convert a MAC address to a number | ||
* | ||
* @param {string} macStr | ||
* @returns {number} | ||
*/ | ||
convertToNumber: function convertToNumber (macStr) { | ||
var splits, macHexStr, separator | ||
var result = 0 | ||
/** | ||
* Convert a MAC address to a number | ||
* | ||
* @param {string} macStr | ||
* @returns {number} | ||
*/ | ||
function convertToNumber (macStr) { | ||
var result, splits, macHexStr, separator | ||
result = 0 | ||
if (typeof macStr === 'string' && | ||
macStr.length === 17) { | ||
// Check which separator is used | ||
if (macStr.indexOf(':') !== -1) { | ||
separator = ':' | ||
} else if (macStr.indexOf('-') !== -1) { | ||
separator = '-' | ||
} | ||
if (typeof macStr === 'string' && | ||
macStr.length === 17) { | ||
// Check which separator is used | ||
if (macStr.indexOf(':') !== -1) { | ||
separator = ':' | ||
} else if (macStr.indexOf('-') !== -1) { | ||
separator = '-' | ||
} | ||
if (separator) { | ||
splits = macStr.split(separator) | ||
if (splits.length === 6) { | ||
macHexStr = splits.join('') | ||
result = parseInt(macHexStr, 16) | ||
} | ||
if (separator) { | ||
splits = macStr.split(separator) | ||
if (splits.length === 6) { | ||
macHexStr = splits.join('') | ||
result = parseInt(macHexStr, 16) | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
return result | ||
} |
{ | ||
"name": "@gidw/mac-address-util", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Utility functions for working with MAC addresses", | ||
@@ -16,3 +16,4 @@ "author": "Gilles De Waele", | ||
"devDependencies": { | ||
"standard": "11.0.1" | ||
"mocha": "7.0.0", | ||
"standard": "14.3.1" | ||
}, | ||
@@ -23,3 +24,6 @@ "repository": "github:GiDW/mac-address-util.git", | ||
"email": "de.waele.gilles@gmail.com" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
} | ||
} |
@@ -6,2 +6,3 @@ # MAC address utilities | ||
[![NPM version](https://img.shields.io/npm/v/@gidw/mac-address-util.svg)](https://www.npmjs.com/package/@gidw/mac-address-util) | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
[![License](https://img.shields.io/github/license/GiDW/mac-address-util.svg)](https://github.com/GiDW/mac-address-util/blob/master/LICENSE) | ||
@@ -12,9 +13,17 @@ | ||
```js | ||
const macAddressUtil = require('@gidw/mac-address-util'); | ||
var macAddressUtil = require('@gidw/mac-address-util') | ||
// Returns "88:77:66:55:44:33" | ||
macAddressUtil.convertToMac(150046399349811); | ||
macAddressUtil.convertToMac(150046399349811) | ||
// Returns "45:67:89:ab:cd:ef" | ||
macAddressUtil.convertToMac(76310993685999) | ||
// Returns "" | ||
macAddressUtil.convertToMac(-1) | ||
// Returns 150046399349811 | ||
macAddressUtil.convertToNumber('88:77:66:55:44:33'); | ||
macAddressUtil.convertToNumber('88:77:66:55:44:33') | ||
// Returns 76310993685999 | ||
macAddressUtil.convertToNumber('45-67-89-AB-cd-Ef') | ||
// Returns 0 | ||
macAddressUtil.convertToNumber('abc') | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6006
6
111
28
2