-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): replace camel- and snake-case keys libs with custom code
fixes #170
- Loading branch information
1 parent
ca95d6a
commit b20e1be
Showing
7 changed files
with
70 additions
and
52 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// @flow | ||
import forEach from 'lodash.foreach' | ||
import camelCase from 'lodash.camelcase' | ||
import isPlainObject from 'lodash.isplainobject' | ||
import snakeCase from 'lodash.snakecase' | ||
|
||
/** | ||
* Converts the keys for an object (or array of objects) using string mapping | ||
* function passed in. Operates on object recursively. | ||
*/ | ||
function mapObjectKeys (object: any, keyMapper: (string) => string): any { | ||
const convertedObject = {} | ||
const convertedArray = [] | ||
forEach( | ||
object, | ||
(value, key) => { | ||
if (isPlainObject(value) || Array.isArray(value)) { | ||
// If plain object or an array, recursively update keys of any values | ||
// that are also objects. | ||
value = mapObjectKeys(value, keyMapper) | ||
} | ||
if (Array.isArray(object)) convertedArray.push(value) | ||
else convertedObject[keyMapper(key)] = value | ||
} | ||
) | ||
if (Array.isArray(object)) return convertedArray | ||
else return convertedObject | ||
} | ||
|
||
/** | ||
* Converts the keys for an object or array of objects to camelCase. The function | ||
* always recursively converts keys. | ||
*/ | ||
export function camelCaseKeys (object: any): any { | ||
return mapObjectKeys(object, camelCase) | ||
} | ||
|
||
/** | ||
* Converts the keys for an object or array of objects to snake_case. The function | ||
* always recursively converts keys. | ||
*/ | ||
export function snakeCaseKeys (object: any): any { | ||
return mapObjectKeys(object, snakeCase) | ||
} |
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
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
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