Last active
August 16, 2022 23:06
-
-
Save jebarjonet/3118b75e62acb1daf387746524e203cc to your computer and use it in GitHub Desktop.
Lodash FP missing functions
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
/** | |
* Returns true if item is defined | |
* @example | |
* isDefined('something') => true | |
*/ | |
export const isDefined = (item: any): boolean => !isNil(item) | |
/** | |
* Scales a value from a range to another range | |
* @example | |
* scale(1, [0, 1], [0, 100]) => 100 | |
* scale(5, [0, 10], [2, 4]) => 3 | |
*/ | |
export const scale = ( | |
value: number, | |
startRange: number[], | |
endRange: number[], | |
): number => { | |
const result = | |
((value - startRange[0]) * (endRange[1] - endRange[0])) / | |
(startRange[1] - startRange[0]) + | |
endRange[0] | |
return result || 0 | |
} | |
/** | |
* Rename a property in object | |
* @example | |
* rename('a', 'b', { a: 1 }) => { b: 1 } | |
*/ | |
export const rename = (fromPath: string, toPath: string, object: Object): Object => | |
set(toPath, object[fromPath], omit([fromPath], object)) | |
/** | |
* Exchange value in fromPath key with value in toPath key | |
* @example | |
* trade('a', 'b', { a: 1, b: 2, c: 3 }) => { a: 2, b: 1, c: 3 } | |
*/ | |
export const trade = (fromPath: string, toPath: string, object: Object): Object => { | |
const fromValue = get(fromPath, object) | |
return flow([rename(toPath, fromPath), set(toPath, fromValue)])(object) | |
} | |
/** | |
* Returns array with value pushed at index | |
* @example | |
* insertAt(1, 9, [1, 2, 3]) => [1, 9, 2, 3] | |
*/ | |
export const insertAt = (index: number, value: any, array: any[]): any[] => { | |
if (index > array.length) { | |
return array | |
} | |
return array.slice(0, index).concat(value, array.slice(index)) | |
} | |
/** | |
* Returns array without the value at index | |
* @example | |
* removeAt(1, [1, 2, 3]) => [1, 3] | |
*/ | |
export const removeAt = (index: number, array: any[]): any[] => | |
array.slice(0, index).concat(array.slice(index + 1)) | |
/** | |
* Returns array with value at index replaced by another value | |
* @example | |
* replaceAt(1, 9, [1, 2, 3]) => [1, 9, 3] | |
*/ | |
export const replaceAt = ( | |
index: number, | |
value: any, | |
array: any[], | |
): any[] => { | |
if (index > array.length) { | |
return array | |
} | |
return array.slice(0, index).concat(value, array.slice(index + 1)) | |
} | |
/** | |
* Returns true if array starts with the other array | |
* @example | |
* startsWith(['a', 'b'], ['a', 'b', 'c']) => true | |
*/ | |
export const startsWith = (source: string[], target: string[]): boolean => | |
source.every((value, index) => value === target[index]) | |
/** | |
* Returns array with item at fromIndex moved to toIndex | |
* @example | |
* moveTo(1, 4, [1, 2, 3, 4, 5, 6]) => [1, 3, 4, 5, 2, 6] | |
*/ | |
export const moveTo = ( | |
fromIndex: number, | |
toIndex: number, | |
array: any[], | |
): any[] => { | |
// indexes are equals | |
if (fromIndex === toIndex) { | |
return array | |
} | |
const item = array[fromIndex] | |
// move down | |
if (fromIndex < toIndex) { | |
return [ | |
...array.slice(0, fromIndex), | |
...array.slice(fromIndex + 1, toIndex + 1), | |
item, | |
...array.slice(toIndex + 1), | |
] | |
} | |
// move up | |
return [ | |
...array.slice(0, toIndex), | |
item, | |
...array.slice(toIndex, fromIndex), | |
...array.slice(fromIndex + 1), | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment