String.prototype.toBase64 = function () { const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; let result = ''; let i = 0; while (i < this.length) { let a = this.charCodeAt(i++) || 0; let b = this.charCodeAt(i++) || 0; let c = this.charCodeAt(i++) || 0; let b1 = (a >> 2) & 0x3F; let b2 = ((a & 0x3) << 4) | ((b >> 4) & 0xF); let b3 = ((b & 0xF) << 2) | ((c >> 6) & 0x3); let b4 = c & 0x3F; if (!b) b3 = b4 = 64; else if (!c) b4 = 64; result += characters.charAt(b1) + characters.charAt(b2) + characters.charAt(b3) + characters.charAt(b4); } return result; } const hexToBase64 = hex => hex .match(/.{1,2}/g) .map(v => String.fromCharCode(parseInt(v, 16))) .join`` .toBase64();
function hexToBase64(hex) {return hex;- String.prototype.toBase64 = function () {
- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
- let result = '';
- let i = 0;
- while (i < this.length) {
- let a = this.charCodeAt(i++) || 0;
- let b = this.charCodeAt(i++) || 0;
- let c = this.charCodeAt(i++) || 0;
- let b1 = (a >> 2) & 0x3F;
- let b2 = ((a & 0x3) << 4) | ((b >> 4) & 0xF);
- let b3 = ((b & 0xF) << 2) | ((c >> 6) & 0x3);
- let b4 = c & 0x3F;
- if (!b) b3 = b4 = 64;
- else if (!c) b4 = 64;
- result += characters.charAt(b1) +
- characters.charAt(b2) +
- characters.charAt(b3) +
- characters.charAt(b4);
- }
- return result;
- }
- const hexToBase64 = hex =>
- hex
- .match(/.{1,2}/g)
- .map(v => String.fromCharCode(parseInt(v, 16)))
- .join``
- .toBase64();
Construct a singleton in js.
Tests:
let obj1 = new Singleton();
let obj2 = new Singleton();
obj1.test = 1;
obj1 === obj2 // true
obj2.test // 1
let Singleton = (function () {
let instance;
return function Construct_singletone () {
if (instance) {
return instance;
}
if (this && this.constructor === Construct_singletone) {
instance = this;
} else {
return new Construct_singletone();
}
}
}());
let obj1 = new Singleton();
let obj2 = new Singleton();
obj1.test = 1;
describe("Test cases", function(){
it("instance must be the same", function(){
Test.assertEquals(obj1 === obj2, true, "Passed");
Test.assertEquals(obj2.test, 1, "Passed");
});
});
var merge = (array1, array2, callbackFN) => callbackFN(array1,array2); callbackFN = (a,b) => Array.isArray(a) && Array.isArray(b) && a.length === b.length ? a.map((c, i) => c + b[i]) : 'both inputs must be Array and same size of elements'; var x = merge([1, 3, 4], [5, 6, 7, 8], callbackFN); console.log(x); //'both inputs must be Array and same size of elements' x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN); console.log(x); //[6, 8, 10, 12].
var merge = function(array1, array2, callbackFN){return callbackFN(array1,array2);}- var merge = (array1, array2, callbackFN) => callbackFN(array1,array2);
function callbackFN(a,b) {if(Array.isArray(a) && Array.isArray(b) && (a.length === b.length)){return a.map(function(c,i){ return c + b[i]; });} else {return 'both inputs must be Array and same size of elements';}}- callbackFN = (a,b) => Array.isArray(a) && Array.isArray(b) && a.length === b.length ?
- a.map((c, i) => c + b[i])
- : 'both inputs must be Array and same size of elements';
- var x = merge([1, 3, 4], [5, 6, 7, 8], callbackFN);
console.log(x); //x should now equal [6, 8, 10, 12].- console.log(x); //'both inputs must be Array and same size of elements'
- x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN);
- console.log(x); //[6, 8, 10, 12].
let greetLanguage = (greeting = "Hello", language = "Javascript") => `${greeting}, ${language}!`;
function greetLanguage(greeting = "Hello", language = "Javascript") {var result = `${greeting}, ${language}!`;console.log(result);return result;}- let greetLanguage = (greeting = "Hello", language = "Javascript") => `${greeting}, ${language}!`;