This file contains 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
const items = [1, 2, 3] | |
try { | |
// you need a semicolon after following line to make it work | |
items.shift() | |
(items.length && items[0] === 2) && items.unshift(1) | |
} catch(e) { | |
console.error(e) | |
} |
This file contains 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
const groupBy = (arr, path) => { | |
if (!path || typeof path !== "string") return {} | |
return Object.keys(arr).reduce((accum, key) => { | |
const value = getProperty(arr[key], path) | |
if (accum[value]) return { ...accum, [value]: [...accum[value], arr[key]] } | |
return { ...accum, [value]: [arr[key]] } | |
}, {}) |
This file contains 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
const getProperty = (obj, path, defaultValue) => { | |
const tokens = path.split(/[.[\]]+/g).filter(Boolean) | |
if (!obj || typeof obj !== "object") return defaultValue | |
if (tokens.length === 1) { | |
return (obj[tokens[0]] === undefined) ? defaultValue : obj[tokens[0]] | |
} | |
return getProperty(obj[tokens[0]], tokens.slice(1).join("."), defaultValue) |
This file contains 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
const match = (fields, key) => { | |
if (["string", "number"].indexOf(typeof fields) !== -1) return fields === key | |
if (fields.indexOf) return fields.indexOf(key) !== -1 | |
return false | |
} | |
const omit = (obj, fields) => Object.keys(obj).reduce((accum, propName) => { | |
if (match(fields, propName)) return accum | |
return { ...accum, [propName]: obj[propName] } |
This file contains 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
const isObject = value => typeof value === "object" | |
const cloneDeep = (value) => { | |
if (!value || !isObject(value)) return value | |
if (Array.isArray(value)) return value.map(item => cloneDeep(item)) | |
return Object.keys(value).reduce((accum, propName) => { | |
const propValue = value[propName] | |