bmax-utils
A utility package with JavaScript helper functions.
Table of Contents
Functions
Dates
Helper functions for working with date objects.
getDayDifferenceBetweenDates
Returns the difference between two dates in days
; const today = ;const tomorrow = ; ;// 1
Helper
A package of helper functions that can vary from very general to very specific use cases.
camelCaseToLowDash
Converts a camel case string to low dash separated string if a string is passed to the function. If an object is passed to the function, it will convert its camelCase keys to low dash separated keys.
; ;// 'hallo_welt' ;// { hallo_welt: 'halloWelt' }
generateOpacity
Generates a valid rgba() css string from a hex, rgb or rgba color with the opacity as second parameter.
; ;// 'rgba(0, 51, 255, 0.6)' ;// 'rgba(0, 51, 255, 0.6)' ;// 'rgba(0, 51, 255, 0.6)'
generateTrigger
Generates trigger of dispatched actions to identify which component triggered an action
A simple but useful function for redux applications.
; const trigger = ; // ... { thisprops;} /*Will dispatch:{ type: 'TEST_ACTION', payload: { id: 1, }, trigger: 'utils/helper/tests/myFunction',}*/
getErrorsFromAjaxOrValidationResponse
/*** Can read error messages from ajax responses in a specific format* Can read error messages from a indicative.js validation response*/ // Ajax response input format:/*{ response: { errors: { items: [ 'My error message 1', 'My error message 2', ], }, }, status: 403,} */ // Validation response format/*[ { field: 'body.username', message: 'The username is required', validation: 'required', }, { field: 'body.password', message: 'The password is required', validation: 'required', },] */ ; /*Options can be:{ ignoreValidationMessages?: boolean; ignoreBackendMessages?: boolean; ignoreStatusCodeMessages?: boolean; defaultValidationMessageIfEmpty?: string; defaultIfEmpty?: string; customStatusCodeMessages?: object; // can be i.e. { 404: 'Not Found' }} */ ;/*Will always return:{ ajax: string[]; // Returns error messages based on HTTP status codes defined in the options all: string[]; // All validation messages backend: string[]; // Messages from a backend response default: string[]; // Default messages (configured in options) validation: string[]; // Messages from an indicative.js validation xhrStatus: number; // The HTTP status code}*/
hexToRgb
Converts 6 digit long hex color to rgb.
; ;// { r: 0, g: 51, b: 255 }
isTrueIsh
Checks if a value is true-ish
; ;// true ;// true ;// true ;// false
lowDashToCamelCase
Converts a low dash separated string to camel case string if a string is passed to the function. If an object is passed to the function, it will convert its low dash separated keys to camelCase keys.
; ;// 'halloWelt' ;// { halloWelt: 'hallo_welt' }
objects
Helper functions for working with objects
blacklist
Returns an Object without the forbidden properties
; ;// { foo: 1 }
defineBlacklist
Defines a function that filters an object to contain no keys matching the strings in the submitted list
; const blacklist = ; ;// { baz: 3 } ;// { fi: 1, fum: 3 }
defineWhitelist
Defines a function that filters an object to contain only keys matching the strings in the submitted list
; const whitelist = ; ;// { foo: 1, bar: 2 } ;// { bar: 2 }
filterForProperty
Filters an object and returns a new object whose properties are the keys of the source object with the filtered prop as value
; ;/*Will return:{ name: 'a', email: 'b', phone: '0',}*/
hasMatchingKeys
Checks if an object has keys that match to the RegExp
; ;// true // false
whitelist
Returns an Object that contains only the allowed properties
; ;// { bar: 2, baz: 3 }
strings
Helper functions for working with strings
formatGetUrlParameters
Formats json params to GET HTTP parameters
; ;// '?number=1234&secret=da39a3ee5e6b4b0d3255bfef95601890afd80709'
replacePlaceholder
Replaces a placeholder string
Placeholders look like: {key}
; ;// 'The id is: 1'