@gidw/mac-address-util
Advanced tools
Comparing version 1.0.0 to 1.0.1
202
index.js
@@ -1,152 +0,76 @@ | ||
'use strict'; | ||
'use strict' | ||
/** | ||
* @module mac-address-util | ||
*/ | ||
module.exports = { | ||
/** | ||
* Convert a numeric MAC address to a standard MAC address notation | ||
* | ||
* @param {number} macNum | ||
* @param {boolean} [debug] | ||
* @returns {string} | ||
*/ | ||
convertToMac: function convertToMac (macNum, debug) { | ||
/** | ||
* 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 = '' | ||
var macStr, macStrLength, macStrLengthDiff, completeMacStr; | ||
var i, padding; | ||
var 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) { | ||
if (macStrLengthDiff === 0) { | ||
completeMacStr = macStr | ||
} else if (macStrLengthDiff > 0 && macStrLengthDiff <= 12) { | ||
padding = '' | ||
for (i = 0; i < macStrLengthDiff; i++) { | ||
padding += '0' | ||
} | ||
completeMacStr = padding + macStr | ||
} | ||
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; | ||
} else { | ||
if (debug) { | ||
console.warn('convertToMac - Invalid MAC number length'); | ||
} | ||
} | ||
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); | ||
} else { | ||
if (debug) { | ||
console.warn( | ||
'convertToMac - Invalid complete MAC string', | ||
completeMacStr | ||
); | ||
} | ||
} | ||
} else { | ||
if (debug) { | ||
console.warn('convertToMac - Invalid MAC number'); | ||
} | ||
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 | ||
* @param {boolean} [debug] | ||
* @returns {number} | ||
*/ | ||
convertToNumber: function convertToNumber (macStr, debug) { | ||
/** | ||
* Convert a MAC address to a number | ||
* | ||
* @param {string} macStr | ||
* @returns {number} | ||
*/ | ||
convertToNumber: function convertToNumber (macStr) { | ||
var splits, macHexStr, separator | ||
var result = 0 | ||
var splits, length, macHexStr; | ||
var result = 0; | ||
var 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 (typeof macStr === 'string' && | ||
macStr.length === 17) { | ||
if (macStr.indexOf(':') !== -1) { | ||
separator = ':'; | ||
} else if (macStr.indexOf('-') !== -1) { | ||
separator = '-'; | ||
} else { | ||
if (debug) { | ||
console.warn( | ||
'convertToNumber - Invalid MAC address' + | ||
' - Unknown separator' | ||
); | ||
} | ||
} | ||
if (separator) { | ||
splits = macStr.split(separator); | ||
length = splits.length; | ||
if (length === 6) { | ||
macHexStr = splits.join(''); | ||
result = parseInt(macHexStr, 16); | ||
} else { | ||
if (debug) { | ||
console.warn( | ||
'convertToNumber - Invalid MAC address' + | ||
' - Invalid split', | ||
splits | ||
); | ||
} | ||
} | ||
} | ||
} else { | ||
if (debug) { | ||
console.warn('convertToNumber - Invalid MAC address'); | ||
} | ||
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.0", | ||
"version": "1.0.1", | ||
"description": "Utility functions for working with MAC addresses", | ||
@@ -15,5 +15,4 @@ "author": "Gilles De Waele", | ||
"types": "index.d.ts", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"eslint": "4.11.0" | ||
"standard": "11.0.1" | ||
}, | ||
@@ -24,6 +23,3 @@ "repository": "github:GiDW/mac-address-util.git", | ||
"email": "de.waele.gilles@gmail.com" | ||
}, | ||
"scripts": { | ||
"lint": "eslint index.js" | ||
} | ||
} |
@@ -6,3 +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) | ||
[![NPM version](https://img.shields.io/github/license/GiDW/mac-address-util.svg)](https://github.com/GiDW/mac-address-util/blob/master/LICENSE) | ||
[![License](https://img.shields.io/github/license/GiDW/mac-address-util.svg)](https://github.com/GiDW/mac-address-util/blob/master/LICENSE) | ||
@@ -9,0 +9,0 @@ ## Usage |
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
4180
68