Skip to content

Commit

Permalink
fix(build): replace camel- and snake-case keys libs with custom code
Browse files Browse the repository at this point in the history
fixes #170
  • Loading branch information
landonreed committed May 17, 2018
1 parent ca95d6a commit b20e1be
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 52 deletions.
44 changes: 44 additions & 0 deletions lib/common/util/map-keys.js
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)
}
2 changes: 1 addition & 1 deletion lib/editor/actions/trip.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {createAction} from 'redux-actions'
import snakeCaseKeys from 'snakecase-keys'
import {snakeCaseKeys} from '../../common/util/map-keys'
import {fetchGraphQL, secureFetch} from '../../common/actions'
import {setErrorMessage} from '../../manager/actions/status'
import {entityIsNew} from '../util/objects'
Expand Down
2 changes: 1 addition & 1 deletion lib/editor/actions/tripPattern.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {createAction} from 'redux-actions'
import snakeCaseKeys from 'snakecase-keys'
import {snakeCaseKeys} from '../../common/util/map-keys'

import {secureFetch} from '../../common/actions'
import {fetchGTFSEntities} from '../../manager/actions/versions'
Expand Down
4 changes: 2 additions & 2 deletions lib/editor/components/timetable/TimetableEditor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import camelcaseKeys from 'camelcase-keys'
import {camelCaseKeys} from '../../../common/util/map-keys'
import clone from 'lodash.clonedeep'
import objectPath from 'object-path'
import React, {Component, PropTypes} from 'react'
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class TimetableEditor extends Component {
// case because that's how the previous GTFS Editor model object fields
// were named. And unlike the other standard tables, trips and stop times
// were handled in the client as camelcase.
const blankStopTime = camelcaseKeys(generateNullProps('stoptime'))
const blankStopTime = camelCaseKeys(generateNullProps('stoptime'))
objectPath.set(newRow, `stopTimes.${i}`, blankStopTime)
}
objectPath.set(newRow, `stopTimes.${i}.stopId`, stop.stopId)
Expand Down
5 changes: 2 additions & 3 deletions lib/editor/util/objects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
import {ENTITY} from '../constants'
import camelcaseKeys from 'camelcase-keys'
import snakeCaseKeys from 'snakecase-keys'
import {camelCaseKeys, snakeCaseKeys} from '../../common/util/map-keys'

import type {Entity} from '../../types'

Expand Down Expand Up @@ -38,7 +37,7 @@ export const getMapFromGtfsStrategy = (component: string) => {
* Converts keys to camelCase and adds shape geometry to trip pattern object.
*/
export function tripPatternToGtfs (tripPattern: any, stops: any): any {
const pattern = camelcaseKeys(tripPattern, {deep: true})
const pattern = camelCaseKeys(tripPattern)
const coordinates = coordinatesFromShapePoints(pattern.shapePoints)
if (coordinates && coordinates.length > 1) {
// Add GeoJSON LineString for coordinates (convenience object used in some
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"auth0-lock": "9",
"babel-polyfill": "^6.22.0",
"bootstrap": "^3.3.7",
"camelcase-keys": "^4.2.0",
"common-tags": "^1.6.0",
"dom-helpers": "3.2.1",
"file-saver": "^1.3.8",
Expand All @@ -43,8 +42,12 @@
"jszip": "^3.0.0",
"jwt-decode": "^2.1.0",
"leaflet": "1.0.3",
"lodash.camelcase": "^4.3.0",
"lodash.clonedeep": "^4.5.0",
"lodash.foreach": "^4.5.0",
"lodash.isplainobject": "^4.0.6",
"lodash.omit": "^4.5.0",
"lodash.snakecase": "^4.1.1",
"lodash.throttle": "^4.1.1",
"lodash.tolower": "^4.1.2",
"lodash.uniqueid": "^4.0.1",
Expand Down Expand Up @@ -83,7 +86,6 @@
"redux-undo": "^1.0.0-beta9-9-7",
"reselect": "^3.0.0",
"shpjs": "^3.3.2",
"snakecase-keys": "^1.1.0",
"truncate": "^2.0.0",
"turf-area": "^3.0.12",
"turf-bbox-polygon": "^3.0.12",
Expand Down
59 changes: 16 additions & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1769,14 +1769,6 @@ camelcase-keys@^2.0.0:
camelcase "^2.0.0"
map-obj "^1.0.0"

camelcase-keys@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/camelcase-keys/-/camelcase-keys-4.2.0.tgz#a2aa5fb1af688758259c32c141426d78923b9b77"
dependencies:
camelcase "^4.1.0"
map-obj "^2.0.0"
quick-lru "^1.0.0"

camelcase@^1.0.2:
version "1.2.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-1.2.1.tgz#9bb5304d2e0b56698b2c758b08a3eaa9daa58a39"
Expand All @@ -1789,10 +1781,6 @@ camelcase@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-3.0.0.tgz#32fc4b9fcdaf845fcdf7e73bb97cac2261f0ab0a"

camelcase@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd"

caniuse-api@^1.5.3:
version "1.6.1"
resolved "https://registry.yarnpkg.com/caniuse-api/-/caniuse-api-1.6.1.tgz#b534e7c734c4f81ec5fbe8aca2ad24354b962c6c"
Expand Down Expand Up @@ -4742,6 +4730,10 @@ lodash.assign@^4.1.0, lodash.assign@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"

lodash.camelcase@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"

lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
Expand All @@ -4750,6 +4742,10 @@ lodash.cond@^4.3.0:
version "4.5.2"
resolved "https://registry.yarnpkg.com/lodash.cond/-/lodash.cond-4.5.2.tgz#f471a1da486be60f6ab955d17115523dd1d255d5"

lodash.foreach@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53"

lodash.isarguments@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
Expand All @@ -4766,6 +4762,10 @@ lodash.isobject@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"

lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"

lodash.keys@^3.0.0, lodash.keys@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a"
Expand Down Expand Up @@ -4810,6 +4810,10 @@ lodash.restparam@^3.0.0:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"

lodash.snakecase@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz#39d714a35357147837aefd64b5dcbb16becd8f8d"

lodash.template@^4.2.4:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.template/-/lodash.template-4.4.0.tgz#e73a0385c8355591746e020b99679c690e68fba0"
Expand Down Expand Up @@ -4913,10 +4917,6 @@ map-obj@^1.0.0, map-obj@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-1.0.1.tgz#d933ceb9205d82bdcf4886f6742bdc2b4dea146d"

map-obj@^2.0.0, map-obj@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-2.0.0.tgz#a65cd29087a92598b8791257a523e021222ac1f9"

map-stream@~0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/map-stream/-/map-stream-0.1.0.tgz#e56aa94c4c8055a16404a0674b78f215f7c8e194"
Expand Down Expand Up @@ -6207,10 +6207,6 @@ querystring@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"

quick-lru@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8"

quickselect@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/quickselect/-/quickselect-1.0.0.tgz#02630818f9aae4ecab26f0103f98d061c17c58f3"
Expand Down Expand Up @@ -7287,13 +7283,6 @@ slide@^1.1.3:
version "1.1.6"
resolved "https://registry.yarnpkg.com/slide/-/slide-1.1.6.tgz#56eb027d65b4d2dce6cb2e2d32c4d4afc9e1d707"

snakecase-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/snakecase-keys/-/snakecase-keys-1.1.0.tgz#1adb17ddf7531c210cc293336f93d0ef1f8e73b8"
dependencies:
map-obj "~2.0.0"
to-snake-case "~0.1.2"

sntp@1.x.x:
version "1.0.9"
resolved "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198"
Expand Down Expand Up @@ -7761,22 +7750,6 @@ to-fast-properties@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47"

to-no-case@0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/to-no-case/-/to-no-case-0.1.1.tgz#cf33c70e0f28168d95e4159abf150e8c542ef9fe"

to-snake-case@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/to-snake-case/-/to-snake-case-0.1.2.tgz#d047b6dbf048dae1bdc260b3c296f54ca34eed7c"
dependencies:
to-space-case "0.1.2"

to-space-case@0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/to-space-case/-/to-space-case-0.1.2.tgz#9a66be3ebe53f2779f687f0262effd1fc5b6d15e"
dependencies:
to-no-case "0.1.1"

tough-cookie@^2.3.2, tough-cookie@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.3.tgz#0b618a5565b6dea90bf3425d04d55edc475a7561"
Expand Down

0 comments on commit b20e1be

Please sign in to comment.