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 deepClone(obj, hash = new Map()) { | |
if (Object(obj) !== obj) return obj; // primitives | |
if (hash.has(obj)) return hash.get(obj); // cyclic reference | |
var result = Array.isArray(obj) ? [] | |
: obj.constructor ? new obj.constructor() : {}; | |
hash.set(obj, result); | |
if (obj instanceof Map) | |
Array.from(obj, ([key, val]) => result.set(key, deepClone(val, hash)) ); | |
return Object.assign(result, ...Object.keys(obj).map ( | |
key => ({ [key]: deepClone(obj[key], hash) }) )); |
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 getByteLen(normal_val) { | |
// Force string type | |
normal_val = String(normal_val); | |
var byteLen = 0; | |
for (var i = 0; i < normal_val.length; i++) { | |
var c = normal_val.charCodeAt(i); | |
byteLen += c < (1 << 7) ? 1 : | |
c < (1 << 11) ? 2 : | |
c < (1 << 16) ? 3 : |
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
// reducer | |
function todosReducer(state, action) { | |
switch (action.type) { | |
case 'add': | |
return [...state, { | |
text: action.text, | |
completed: false | |
}]; | |
// ... other actions ... | |
default: |
OlderNewer