From: Lady Date: Sat, 24 Jun 2023 23:54:50 +0000 (-0700) Subject: Use === more frequently in binary.js X-Git-Tag: 0.4.0~9 X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/a06cc433046c9605fb750a5e2e03cc2b628812a6?ds=sidebyside Use === more frequently in binary.js The convention is to use `===` in situations where type·casting is known not occur. --- diff --git a/binary.js b/binary.js index d5642bd..deb13db 100644 --- a/binary.js +++ b/binary.js @@ -96,7 +96,7 @@ const bufferFromArgs = ($, $s) => { ), [], ))( - typeof $ == "string" + typeof $ === "string" ? $ : hasOwnProperty($, "raw") ? rawString( @@ -138,7 +138,7 @@ const decodeBase16 = (source) => { }, ); const { length } = u4s; - if (length % 2 == 1) { + if (length % 2 === 1) { // The length is such that an entire letter would be dropped during // a forgiving decode. throw new RangeError( @@ -171,7 +171,7 @@ const decodeBase32 = (source, wrmg) => { const u5s = map( wrmg ? stringReplace(source, /-/gu, "") - : source.length % 8 == 0 + : source.length % 8 === 0 ? stringReplace(source, /(?:=|={3,4}|={6})$/u, "") : source, (ucsCharacter) => { @@ -181,15 +181,15 @@ const decodeBase32 = (source, wrmg) => { ? code - 48 : code >= 0x41 && code <= 0x48 ? code - 55 - : code == 0x49 + : code === 0x49 ? 1 // I : code >= 0x4A && code <= 0x4B ? code - 56 - : code == 0x4C + : code === 0x4C ? 1 // L : code >= 0x4D && code <= 0x4E ? code - 57 - : code == 0x4F + : code === 0x4F ? 0 // O : code >= 0x50 && code <= 0x54 ? code - 58 @@ -198,15 +198,15 @@ const decodeBase32 = (source, wrmg) => { ? code - 59 : code >= 0x61 && code <= 0x68 ? code - 87 - : code == 0x69 + : code === 0x69 ? 1 // i : code >= 0x6A && code <= 0x6B ? code - 88 - : code == 0x6C + : code === 0x6C ? 1 // l : code >= 0x6D && code <= 0x6E ? code - 89 - : code == 0x6F + : code === 0x6F ? 0 // o : code >= 0x70 && code <= 0x74 ? code - 90 @@ -232,7 +232,7 @@ const decodeBase32 = (source, wrmg) => { ); const { length } = u5s; const lengthMod8 = length % 8; - if (lengthMod8 == 1 || lengthMod8 == 3 || lengthMod8 == 6) { + if (lengthMod8 === 1 || lengthMod8 === 3 || lengthMod8 === 6) { // The length is such that an entire letter would be dropped during // a forgiving decode. throw new RangeError( @@ -247,27 +247,27 @@ const decodeBase32 = (source, wrmg) => { // decode” behaviour specified by WhatW·G for base64. const dataIndex = ceil(index * 5 / 8); const remainder = index % 8; - if (remainder == 0) { + if (remainder === 0) { call(viewSetUint8, dataView, [ dataIndex, u5s[index] << 3 | u5s[++index] >> 2, ]); - } else if (remainder == 1) { + } else if (remainder === 1) { call(viewSetUint8, dataView, [ dataIndex, u5s[index] << 6 | u5s[++index] << 1 | u5s[++index] >> 4, ]); - } else if (remainder == 3) { + } else if (remainder === 3) { call(viewSetUint8, dataView, [ dataIndex, u5s[index] << 4 | u5s[++index] >> 1, ]); - } else if (remainder == 4) { + } else if (remainder === 4) { call(viewSetUint8, dataView, [ dataIndex, u5s[index] << 7 | u5s[++index] << 2 | u5s[++index] >> 3, ]); - } else { // remainder == 6 + } else { // remainder === 6 call(viewSetUint8, dataView, [ dataIndex, u5s[index] << 5 | u5s[++index, index++], @@ -286,7 +286,7 @@ const decodeBase32 = (source, wrmg) => { */ const decodeBase64 = (source, safe = false) => { const u6s = map( - source.length % 4 == 0 + source.length % 4 === 0 ? stringReplace(source, /={1,2}$/u, "") : source, (ucsCharacter) => { @@ -297,9 +297,9 @@ const decodeBase64 = (source, safe = false) => { ? code - 71 : code >= 0x30 && code <= 0x39 ? code + 4 - : code == (safe ? 0x2D : 0x2B) + : code === (safe ? 0x2D : 0x2B) ? 62 - : code == (safe ? 0x5F : 0x2F) + : code === (safe ? 0x5F : 0x2F) ? 63 : -1; if (result < 0) { @@ -312,7 +312,7 @@ const decodeBase64 = (source, safe = false) => { }, ); const { length } = u6s; - if (length % 4 == 1) { + if (length % 4 === 1) { // The length is such that an entire letter would be dropped during // a forgiving decode. throw new RangeError( @@ -327,17 +327,17 @@ const decodeBase64 = (source, safe = false) => { // decode” behaviour specified by WhatW·G for base64. const dataIndex = ceil(index * 3 / 4); const remainder = index % 4; - if (remainder == 0) { + if (remainder === 0) { call(viewSetUint8, dataView, [ dataIndex, u6s[index] << 2 | u6s[++index] >> 4, ]); - } else if (remainder == 1) { + } else if (remainder === 1) { call(viewSetUint8, dataView, [ dataIndex, u6s[index] << 4 | u6s[++index] >> 2, ]); - } else { // remainder == 2 + } else { // remainder === 2 call(viewSetUint8, dataView, [ dataIndex, u6s[index] << 6 | u6s[++index, index++], @@ -420,21 +420,21 @@ const encodeBase32 = (buffer, wrmg = false) => { // digits 1, 3, 4 & 6 span multiple bytes ? call(viewGetUint8, dataView, [index]) : 0; - const u5 = remainder == 0 + const u5 = remainder === 0 ? currentByte >> 3 - : remainder == 1 + : remainder === 1 ? (currentByte & 0b00000111) << 2 | nextByte >> 6 - : remainder == 2 + : remainder === 2 ? (currentByte & 0b00111111) >> 1 - : remainder == 3 + : remainder === 3 ? (currentByte & 0b00000001) << 4 | nextByte >> 4 - : remainder == 4 + : remainder === 4 ? (currentByte & 0b00001111) << 1 | nextByte >> 7 - : remainder == 5 + : remainder === 5 ? (currentByte & 0b01111111) >> 2 - : remainder == 6 + : remainder === 6 ? (currentByte & 0b00000011) << 3 | nextByte >> 5 - : (++index, currentByte & 0b00011111); // remainder == 7 + : (++index, currentByte & 0b00011111); // remainder === 7 const result = wrmg ? u5 < 10 ? u5 + 48 : u5 < 18 ? u5 + 55 @@ -491,7 +491,7 @@ const encodeBase64 = (buffer, safe = false) => { for (let index = 0; index < byteLength;) { const codeUnitIndex = ceil(index * 4 / 3); const currentIndex = codeUnitIndex + +( - index % 3 == 0 && resultingCodeUnits[codeUnitIndex] != 0x3D + index % 3 === 0 && resultingCodeUnits[codeUnitIndex] != 0x3D ); // every third byte handles two letters; this is for the second const remainder = currentIndex % 4; const currentByte = call(viewGetUint8, dataView, [index]); @@ -499,13 +499,13 @@ const encodeBase64 = (buffer, safe = false) => { // digits 1 & 2 span multiple bytes ? call(viewGetUint8, dataView, [index]) : 0; - const u6 = remainder == 0 + const u6 = remainder === 0 ? currentByte >> 2 - : remainder == 1 + : remainder === 1 ? (currentByte & 0b00000011) << 4 | nextByte >> 4 - : remainder == 2 + : remainder === 2 ? (currentByte & 0b00001111) << 2 | nextByte >> 6 - : (++index, currentByte & 0b00111111); // remainder == 3 + : (++index, currentByte & 0b00111111); // remainder === 3 const result = u6 < 26 ? u6 + 65 : u6 < 52 @@ -534,7 +534,7 @@ const encodeBase64 = (buffer, safe = false) => { */ const sourceFromArgs = ($, $s) => stringReplace( - typeof $ == "string" ? $ : hasOwnProperty($, "raw") + typeof $ === "string" ? $ : hasOwnProperty($, "raw") ? rawString( $, ...objectCreate(argumentIterablePrototype, { @@ -666,8 +666,8 @@ export const isBase16 = ($) => { return false; } else { const source = stringReplace($, /[\t\n\f\r ]+/gu, ""); - return source.length % 2 != 1 && - call(reExec, /[^0-9A-F]/iu, [source]) == null; + return source.length % 2 !== 1 && + call(reExec, /[^0-9A-F]/iu, [source]) === null; } }; @@ -682,11 +682,11 @@ export const isBase32 = ($) => { return false; } else { const source = stringReplace($, /[\t\n\f\r ]+/gu, ""); - const trimmed = source.length % 8 == 0 + const trimmed = source.length % 8 === 0 ? stringReplace(source, /(?:=|={3,4}|={6})$/u, "") : source; - return trimmed.length % 8 != 1 && - call(reExec, /[^2-7A-Z/]/iu, [trimmed]) == null; + return trimmed.length % 8 !== 1 && + call(reExec, /[^2-7A-Z/]/iu, [trimmed]) === null; } }; @@ -701,11 +701,11 @@ export const isBase64 = ($) => { return false; } else { const source = stringReplace($, /[\t\n\f\r ]+/gu, ""); - const trimmed = source.length % 4 == 0 + const trimmed = source.length % 4 === 0 ? stringReplace(source, /={1,2}$/u, "") : source; - return trimmed.length % 4 != 1 && - call(reExec, /[^0-9A-Za-z+\/]/u, [trimmed]) == null; + return trimmed.length % 4 !== 1 && + call(reExec, /[^0-9A-Za-z+\/]/u, [trimmed]) === null; } }; @@ -732,11 +732,11 @@ export const isFilenameSafeBase64 = ($) => { return false; } else { const source = stringReplace($, /[\t\n\f\r ]+/gu, ""); - const trimmed = source.length % 4 == 0 + const trimmed = source.length % 4 === 0 ? stringReplace(source, /={1,2}$/u, "") : source; - return trimmed.length % 4 != 1 && - call(reExec, /[^0-9A-Za-z_-]/u, [trimmed]) == null; + return trimmed.length % 4 !== 1 && + call(reExec, /[^0-9A-Za-z_-]/u, [trimmed]) === null; } }; @@ -765,8 +765,8 @@ export const isWRMGBase32 = ($) => { } else { const source = stringReplace($, /[\t\n\f\r ]+/gu, ""); const trimmed = stringReplace(source, /-/gu, ""); - return trimmed.length % 8 != 1 && - call(reExec, /[^0-9A-TV-Z]/iu, [trimmed]) == null; + return trimmed.length % 8 !== 1 && + call(reExec, /[^0-9A-TV-Z]/iu, [trimmed]) === null; } };