-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(traverse-keys): add keysOfValuesToIgnore option as a workaround
- Loading branch information
Showing
3 changed files
with
29 additions
and
25 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,47 @@ | ||
import * as R from 'ramda' | ||
import { snakeToPascal, pascalToSnake } from './string' | ||
|
||
export const pause = duration => new Promise(resolve => setTimeout(resolve, duration)) | ||
|
||
export const mapObject = (object, fn) => Object.fromEntries(Object.entries(object).map(fn)) | ||
export const filterObject = (object, fn) => Object.fromEntries(Object.entries(object).filter(fn)) | ||
|
||
/** | ||
* Key traversal metafunction | ||
* @static | ||
* @function | ||
* @rtype (fn: (s: String) => String) => (o: Object) => Object | ||
* @param {Function} fn - Key transformation function | ||
* @param {Object} o - Object to traverse | ||
* @param {Object} object - Object to traverse | ||
* @param {Array} [keysOfValuesToIgnore] - Workaround to fix serialisation | ||
* @return {Object} Transformed object | ||
*/ | ||
export const traverseKeys = R.curry((fn, o) => { | ||
const dispatch = { | ||
Object: o => R.fromPairs(R.toPairs(o).map(function (arr) { | ||
const k = arr[0] | ||
const v = arr[1] | ||
return [fn(k), traverseKeys(fn, v)] | ||
})), | ||
Array: o => o.map(traverseKeys(fn)) | ||
} | ||
|
||
return (dispatch[R.type(o)] || R.identity)(o) | ||
}) | ||
export const traverseKeys = (fn, object, keysOfValuesToIgnore = []) => { | ||
if (typeof object !== 'object') return object | ||
if (Array.isArray(object)) return object.map(i => traverseKeys(fn, i, keysOfValuesToIgnore)) | ||
return mapObject(object, ([key, value]) => [ | ||
fn(key), | ||
keysOfValuesToIgnore.includes(key) ? value : traverseKeys(fn, value, keysOfValuesToIgnore) | ||
]) | ||
} | ||
|
||
/** | ||
* snake_case key traversal | ||
* @static | ||
* @rtype (o: Object) => Object | ||
* @param {Object} o - Object to traverse | ||
* @param {Object} object - Object to traverse | ||
* @param {Array} keysOfValuesToIgnore | ||
* @return {Object} Transformed object | ||
* @see pascalToSnake | ||
*/ | ||
export const snakizeKeys = o => traverseKeys(pascalToSnake, o) | ||
export const snakizeKeys = traverseKeys.bind(null, pascalToSnake) | ||
|
||
/** | ||
* PascalCase key traversal | ||
* @static | ||
* @rtype (o: Object) => Object | ||
* @param {Object} o - Object to traverse | ||
* @param {Object} object - Object to traverse | ||
* @param {Array} keysOfValuesToIgnore | ||
* @return {Object} Transformed object | ||
* @see snakeToPascal | ||
*/ | ||
export const pascalizeKeys = o => traverseKeys(snakeToPascal, o) | ||
export const pascalizeKeys = traverseKeys.bind(null, snakeToPascal) |
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
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