Created
February 24, 2018 11:52
-
-
Save Floofies/bed7b0fb69f2aecbb2fc5c8dbf6f42f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isFloat(int) { | |
return int % 1 !== 0; | |
} | |
function padBinary(string, pad = 7) { | |
while(string.length < pad) string = "0".concat(string); | |
return string; | |
} | |
if ((typeof window) === "undefined" || !("btoa" in window)) var btoa = v => Buffer.from(v).toString("base64"); | |
if ((typeof window) === "undefined" || !("atob" in window)) var atob = v => Buffer.from(v, "base64").toString(); | |
const toBase64 = btoa; | |
const fromBase64 = atob; | |
// Convert a number or single letter to a Base 2 integer. | |
function base2(value) { | |
if ((typeof value) === "string") { | |
value = value.charCodeAt(0); | |
} | |
if ((typeof value) === "number") { | |
return padBinary(value.toString(2)); | |
} | |
} | |
const supportedRegExpProps = { | |
sticky: "sticky" in RegExp.prototype, | |
unicode: "unicode" in RegExp.prototype, | |
flags: "flags" in RegExp.prototype | |
}; | |
// Returns a non-delimited string of bytes. | |
// <Property Length, Value Length, Prop Bytes, Value Bytes> | |
function objectToBytes(obj) { | |
if (Array.isArray(obj)) { | |
return Array.from(obj, toBytes); | |
} else if (obj instanceof RegExp) { | |
var flags = ""; | |
if (supportedRegExpProps.flags) { | |
flags = obj.flags; | |
} else { | |
if (obj.global) flags += "g"; | |
if (obj.ignorecase) flags += "i"; | |
if (obj.multiline) flags += "m"; | |
if (supportedRegExpProps.sticky && obj.sticky) flags += "y"; | |
if (supportedRegExpProps.unicode && obj.unicode) flags += "u"; | |
} | |
return objectToBytes({ | |
source: obj.source, | |
flags: flags | |
}); | |
} else { | |
const props = Object.keys(obj).sort(); | |
return props.map(function (prop) { | |
const propBytes = toBytes(prop); | |
const valueBytes = toBytes(obj[prop]); | |
const propLength = base2(propBytes.length); | |
const valueLength = base2(valueBytes.length); | |
return [propLength, valueLength, propBytes, valueBytes]; | |
}); | |
} | |
} | |
function toBytes(value) { | |
if (value === null) { | |
return toBase64(base2(0)); | |
} | |
var type = (typeof value); | |
if (type === "object") { | |
return objectToBytes(value); | |
} | |
if (type == "boolean") { | |
value = value.toString(); | |
type = "string"; | |
} | |
if (type === "string") { | |
return Array.from(toBase64(value), base2); | |
} | |
if (type === "number") { | |
return base2(toBase64(value.toString(10))); | |
} | |
} | |
function bytesToObject(bytes) { | |
if (Array.isArray(bytes)) { | |
} | |
} | |
function fromBytes(bytes) { | |
if ((typeof bytes) === "string") { | |
var value = ""; | |
for (var loc = 0; loc < bytes.length; loc += 7) { | |
for (var bLoc = loc; loc - bLoc < 7; bLoc++) { | |
value += fromBase64(String.fromCharCode(parseInt(bytes[loc], 2))); | |
} | |
} | |
return value; | |
} | |
if (Array.isArray(bytes)) { | |
return Array.from(bytes).map(fromBytes); | |
} | |
} | |
function countAlphabet(string) { | |
const counts = {}; | |
const symbols = Array.from(string, symbol => symbol.charCodeAt(0)).sort((t1, t2) => t1 > t2); | |
symbols.forEach(function (t) { | |
const char = String.fromCharCode(t); | |
if (!(char in counts)) counts[char] = 0; | |
counts[char]++; | |
}); | |
symbols.filter((symbol, loc, symbols) => loc === 0 || symbol !== symbols[loc - 1]); | |
return { | |
counts: counts, | |
symbols: symbols | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment