Grab bag of utilities for vandelay transform functions.
npm install vandelay-util --save
Utility functions will return undefined when given null or invalid values when appropriate.
For example:
const row = {
name: 'pear'
}
util.capitalize(row.notes) // undefined
util.convert(row.speed).from('m/h').to('km/h') // undefined
Stringifies all provided values and turns them into a deterministic GUID. Null values are ignored.
util.guid('Michael Scott', 'Birthday', null, 1993) // f8e61be1-0b1c-4e4f-81b6-050ae9b8a049
Stringifies all provided values and turns them into a lowercase slug. Null values are ignored.
Note: This function will remove many characters, including non-english characters. Use with caution if you are using this for a unique ID.
util.slug('Michael Scott', 'Birthday', null, 1993) // michael-scott-birthday-1993
Trims, lowercases, and removes redundant whitespace from the given value.
util.normalize('\t HEY \n THANKS') // hey thanks
Capitalizes the first word in the given value.
Capitalizes every word in the given value.
Camelcases the given value.
Converts camelcase to non-camelcase - separated by spaces.
Normalizes any phone number in the world. Country is any three letter country code, defaulting to 'USA'
. If the number includes a country code, you do not need to provide a country
argument.
util.phone(' 123 ') // undefined
util.phone(123) // undefined
// USA numbers
util.phone('4805335949') // +14805335949
util.phone('(480) 533-5949') // +14805335949
util.phone('480-533-5949') // +14805335949
util.phone(4805335949) // +14805335949
// Hong Kong numbers
util.phone('6123-6123') // undefined
util.phone('6123-6123', 'HKG') // +85261236123
util.phone('+852 6123-6123') // +85261236123
})
Exposes convert-units, with additional handling of null and invalid values. For a full list of convertible units, see this page.
util.convert(null).from('m/h').to('km/h') // undefined
util.convert('1').from('F').to('C') // -17.22222222222222
util.convert(1).from('mi').to('km') // 1.6093439485009937
Exposes moment + moment-timezone, with additional handling of null and invalid values. For a full list of time zones, see this page.
util.moment(null) // undefined
util.moment('12/27/1993') // moment object
util.moment.tz('12/27/1993', 'America/New_York') // moment object
Exposes memoization helpers, with additional handling of null and invalid values. For a full usage guide, see this page.
// Fetch a request only once
const getLocationData = util.memo.promise(async () =>
util.request.get('http://google.com/fetch-only-once.json')
)
// Cache synchronous computation
const getTotal = util.memo((data) => {
return data
.map((i) => i.count)
.reduce((prev, curr) =>
prev + curr
, 0)
})
Wrapper around moment.js moment.tz(date, timezone)
that handles null and invalid values. Search timezones here.
util.date('abc', 'America/New_York') // undefined
util.date(null, 'America/Los_Angeles') // undefined
util.date('12/27/1993', 'America/New_York') // 1993-12-27T05:00:00.000Z
Wrapper around number parsing that handles null and invalid values.
util.number('$100.56') // 100.56
util.number(100.56) // 100.56
util.number('costs $100.56') // 100.56
util.number('it is -100.56') // -100.56
Returns true if the coordinate is in the sea. Value can be either [ lon, lat ]
or a GeoJSON Point.
util.geo.isSea([ 0, 0 ]) // true
Returns the timezone for a given point. Value returned can be used as the timezone for util.date
. Value can be either [ lon, lat ]
or a GeoJSON Point.
util.geo.tz([ -118.250587 , 34.031179 ]) // America/Los_Angeles
util.geo.tz({
type: 'Point',
coordinates: [ 121.456424, 31.224417 ]
}) // Asia/Shanghai
Parses GeoJSON and reprojects it into WGS84. If the GeoJSON has a crs
attribute it will auto-detect it, otherwise you can specify it as the second argument.
// Auto-detection based on the input data
util.geo.reproject({
type: 'Point',
crs: {
type: 'name',
properties: {
name: 'EPSG:3006'
}
},
coordinates: [
319180,
6399862
]
}) // { type: 'Point', coordinates: [ 11.965261850066433, 57.704505637111694 ] }
// Manually specifying it, any EPSG code or URN is acceptable
util.geo.reproject({
type: 'Point',
coordinates: [
319180,
6399862
]
}, 'EPSG:3006') // { type: 'Point', coordinates: [ 11.965261850066433, 57.704505637111694 ] }
Converts any given GeoJSON geometry to a Multi
version. Values that are already a Multi
geometry will be returned with no modifications.
util.geo.multi({
type: 'Polygon',
coordinates: [
[ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ],
[ [ 100.2, 0.2 ], [ 100.8, 0.2 ], [ 100.8, 0.8 ], [ 100.2, 0.8 ], [ 100.2, 0.2 ] ]
]
})
Simplifies any given GeoJSON geometry to the precision of a meter. Values that do not need simplification will be returned with no modifications.
util.geo.simplify({
type: 'Polygon',
coordinates: [
[ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ],
[ [ 100.2, 0.2 ], [ 100.8, 0.2 ], [ 100.8, 0.8 ], [ 100.2, 0.8 ], [ 100.2, 0.2 ] ]
]
})
Converts Well-Known-Text to GeoJSON - supports WKT/WKB/EWKT/EWKB/TWKB.
util.geo.wk2JSON('POINT(1 2)') // { type: 'Point', coordinates: [1, 2] }
Returns a geopoint for an intersection. For example:
util.geo.intersection({
intersection: '12th Ave./Hammond St.',
city: 'Lost Wages',
region: 'NV',
country: 'USA'
})
util.geo.intersection({
intersection: '24th Ave., Avery St.',
city: 'Lost Wages',
region: 'NV',
country: 'USA'
})
util.geo.locate({ address, city, region, postalCode, country, minConfidence, filter, sources, layers })
Exposes turf with no modifications.
Exposes lodash with no modifications.
Exposes validator with no modifications.
Exposes superagent with no modifications.
Exposes stae js-sdk with no modifications.