Skip to content

Commit 17f4930

Browse files
committed
fixup: address comments
1 parent f7c1aed commit 17f4930

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lib/os.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@ function endianness() {
143143
}
144144
endianness[Symbol.toPrimitive] = () => kEndianness;
145145

146-
function countOnes(n) {
146+
// Returns the number of ones in the binary representation of the decimal
147+
// number.
148+
function countBinaryOnes(n) {
147149
let count = 0;
150+
// Remove one "1" bit from n until n is the power of 2. This iterates k times
151+
// while k is the number of "1" in the binary representation.
152+
// For more check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
148153
while (n !== 0) {
149154
n = n & (n - 1);
150155
count++;
@@ -166,17 +171,17 @@ function getCIDR({ address, netmask, family }) {
166171
}
167172

168173
const parts = netmask.split(split);
169-
for (const part of parts) {
170-
if (part !== '') {
171-
const binary = parseInt(part, range);
172-
const tmp = countOnes(binary);
174+
for (var i = 0; i < parts.length; i++) {
175+
if (parts[i] !== '') {
176+
const binary = parseInt(parts[i], range);
177+
const tmp = countBinaryOnes(binary);
173178
ones += tmp;
174179
if (hasZeros) {
175180
if (tmp !== 0) {
176181
return null;
177182
}
178183
} else if (tmp !== groupLength) {
179-
if (binary % 2 !== 0) {
184+
if ((binary & 1) !== 0) {
180185
return null;
181186
}
182187
hasZeros = true;
@@ -190,9 +195,11 @@ function getCIDR({ address, netmask, family }) {
190195
function networkInterfaces() {
191196
const interfaceAddresses = getInterfaceAddresses();
192197

193-
for (const val of Object.values(interfaceAddresses)) {
194-
for (const v of val) {
195-
v.cidr = getCIDR(v);
198+
const keys = Object.keys(interfaceAddresses);
199+
for (var i = 0; i < keys.length; i++) {
200+
const arr = interfaceAddresses[keys[i]];
201+
for (var j = 0; j < arr.length; j++) {
202+
arr[j].cidr = getCIDR(arr[j]);
196203
}
197204
}
198205

0 commit comments

Comments
 (0)