helfer

useful helper functions

Usage no npm install needed!

<script type="module">
  import helfer from 'https://cdn.skypack.dev/helfer';
</script>

README

helfer

NPM Package Build Status Dependencies

useful helper functions

helfer is german for helper

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. returns arg if it is an array. returns [arg] otherwise. returns [] if arg is null or undefined.
  • findIndex(array, predicate) -> returns the index of the first array element for which predicate returns true. otherwise returns -1.
  • findIndexWhereProperty(objects, property) returns the index of the first of objects for which property is not undefined. otherwise returns -1.
  • findIndexOfSequence(array, sequence) -> returns the index of the first occurence of sequence in array. otherwise returns -1
  • splitArrayWhere(array, predicate) -> split array into two parts: the first part contains all elements up to (but not including) the first element for which predicate returned true. the second part contains all elements from (and including) the first element for which preducate returned true.
  • splitArrayWhereSequence(array, sequence) -> split array on each occurence of sequence in array

object

  • inherits(constructor, superConstructor) makes an object that has superConstructor as its prototype the prototype of constructor (portable). useful to create error hierarchies that can work with bluebird.catch and helfer.isError.

function

  • parseFunctionArguments(function(a, b, c) {}) -> ['a', 'b', 'c'] the names of the functions arguments
  • identity(value) -> returns value

predicate

  • isObject(value) -> returns boolean whether value is an object
  • isUndefined(value) -> returns boolean whether value is undefined
  • isNull(value) -> returns boolean whether value is null
  • isExisting(value) -> returns boolean whether value is neither null nor undefined
  • isThenable(value) -> returns boolean whether value is a thenable (promise)
  • isError(value) -> returns boolean whether value is instanceof Error

license: MIT