helfer
v0.1.0
Published
useful helper functions
Downloads
13
Readme
helfer
useful helper functions
helfer
is german forhelper
a place for helper functions that are generic enough to be used in more than
one of my npm packages but specific enough not to be
present in utility libraries like lodash.
it aims to have no dependencies, which means that it must reimplement some lodash
functions (findIndex
for example) that some helfer
functions depend on.
at the moment it contains mostly string and array helpers.
see a list of all functions below.
this is a work in progress. it is well tested but the API should not be considered stable. for the moment i'll make breaking changes between versions without warning. i might extract sets of functions into separate packages in the future.
npm install helfer
bower install helfer
var helfer = require('helfer');
lib/helfer.js supports AMD.
if AMD is not available it sets the global variable helfer
.
also look at the tests (and code) for documentation.
string
camelToSnake('camelCase')
->'camel_case'
snakeToCamel('snake_case')
->'snakeCase'
camelToHyphen('camelCase')
->'camel-case'
hyphenToCamel('hyphen-delimited')
->'hyphenDelimited'
colonToSnake('colon:delimited')
->'colon_delimited'
snakeToColon('snake_case')
->'snake:case'
hyphenColonToCamelSnake('hyphen:colon-to:camel-snake')
->'hyphen_colonTo_camelSnake'
camelSnakeToHyphenColon('camel_snakeTo_hyphenColon')
->'camel:snake-to:hyphen-colon'
uppercaseFirstLetter('foo')
->'Foo'
lowercaseFirstLetter('FOO')
->'fOO'
splitCamelcase('oneTwoThree')
->['one', 'two', 'three']
joinCamelcase(['One', 'two', 'three'])
->'oneTwoThree'
splitUnderscore('one_two_three')
->['one', 'two', 'three']
joinUnderscore(['one', 'two', 'three'])
->'one_two_three'
splitUppercaseUnderscore('ONE_TWO_THREE')
->['one', 'two', 'three']
joinUppercaseUnderscore(['one', 'two', 'three'])
->'ONE_TWO_THREE'
array
coerceToArray(array | value | null | undefined)
-> always returns an array. returnsarg
if it is an array. returns[arg]
otherwise. returns[]
ifarg
isnull
orundefined
.findIndex(array, predicate)
-> returns the index of the firstarray
element for whichpredicate
returns true. otherwise returns-1
.findIndexWhereProperty(objects, property)
returns the index of the first ofobjects
for whichproperty
is notundefined
. otherwise returns-1
.findIndexOfSequence(array, sequence)
-> returns the index of the first occurence ofsequence
inarray
. otherwise returns-1
splitArrayWhere(array, predicate)
-> splitarray
into two parts: the first part contains all elements up to (but not including) the first element for whichpredicate
returnedtrue
. the second part contains all elements from (and including) the first element for whichpreducate
returnedtrue
.splitArrayWhereSequence(array, sequence)
-> splitarray
on each occurence ofsequence
inarray
object
inherits(constructor, superConstructor)
makes an object that hassuperConstructor
as its prototype the prototype ofconstructor
(portable). useful to create error hierarchies that can work withbluebird.catch
andhelfer.isError
.
function
parseFunctionArguments(function(a, b, c) {})
->['a', 'b', 'c']
the names of the functions argumentsidentity(value)
-> returnsvalue
predicate
isObject(value)
-> returns boolean whethervalue
is an objectisUndefined(value)
-> returns boolean whethervalue
isundefined
isNull(value)
-> returns boolean whethervalue
isnull
isExisting(value)
-> returns boolean whethervalue
is neithernull
norundefined
isThenable(value)
-> returns boolean whethervalue
is a thenable (promise)isError(value)
-> returns boolean whethervalue
isinstanceof Error