helfer v0.1.0
helfer
useful helper functions
helferis 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 helferbower install helfervar 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. returnsargif it is an array. returns[arg]otherwise. returns[]ifargisnullorundefined.findIndex(array, predicate)-> returns the index of the firstarrayelement for whichpredicatereturns true. otherwise returns-1.findIndexWhereProperty(objects, property)returns the index of the first ofobjectsfor whichpropertyis notundefined. otherwise returns-1.findIndexOfSequence(array, sequence)-> returns the index of the first occurence ofsequenceinarray. otherwise returns-1splitArrayWhere(array, predicate)-> splitarrayinto two parts: the first part contains all elements up to (but not including) the first element for whichpredicatereturnedtrue. the second part contains all elements from (and including) the first element for whichpreducatereturnedtrue.splitArrayWhereSequence(array, sequence)-> splitarrayon each occurence ofsequenceinarray
object
inherits(constructor, superConstructor)makes an object that hassuperConstructoras its prototype the prototype ofconstructor(portable). useful to create error hierarchies that can work withbluebird.catchandhelfer.isError.
function
parseFunctionArguments(function(a, b, c) {})->['a', 'b', 'c']the names of the functions argumentsidentity(value)-> returnsvalue
predicate
isObject(value)-> returns boolean whethervalueis an objectisUndefined(value)-> returns boolean whethervalueisundefinedisNull(value)-> returns boolean whethervalueisnullisExisting(value)-> returns boolean whethervalueis neithernullnorundefinedisThenable(value)-> returns boolean whethervalueis a thenable (promise)isError(value)-> returns boolean whethervalueisinstanceof Error
license: MIT
10 years ago