@fr0st/core v2.1.8
FrostCore
FrostCore is a free, open-source utility library for JavaScript.
It is a tiny (~2kb gzipped) and modern library, providing various methods for manipulating arrays, functions, objects & more.
Table Of Contents
Installation
In Browser
<script type="text/javascript" src="/path/to/frost-core.min.js"></script>Using NPM
npm i @fr0st/coreIn Node.js:
import * as _ from '@fr0st/core';Arrays
Difference
Create a new array containing the values of the first array, that do not exist in any of the additional passed arrays.
arrayis the array you wish to filter.
Any additional arguments supplied will be used to test for the values of the first array.
const diff = _.diff(array, ...arrays);Intersect
Create a new array containing the unique values that exist in all of the passed arrays.
All arguments supplied to this method will be tested for intersections.
const intersect = _.intersect(...arrays);Merge
Merge the values from one or more arrays or array-like objects onto an array.
arrayis the array you wish to merge onto.
Any additional arguments supplied will be merged onto the first array.
_.merge(array, ...arrays);Random Value
Return a random value from an array.
arrayis the array you wish to retrieve a value from.
const randomValue = _.randomValue(array);Range
Return an array containing a range of values.
startis a number indicating the first value of the sequence.endis a number indicating where the sequence will end.stepis a number indicating the increment between values in the sequence, and will default to 1.
const range = _.range(start, end, step);Unique
Remove duplicate elements in an array.
arrayis the array you wish to remove duplicates from.
const unique = _.unique(array);Wrap
Create an array from any value.
valueis the value you wish to create an array from.
const array = _.wrap(value);Functions
Animation
Create a wrapped version of a function that executes at most once per animation frame (using the most recent arguments passed to it).
callbackis the function you wish to wrap.optionsis an object containing options for executing the function.leadingis a boolean indicating whether you wish the function to execute on the leading edge of the animation frame, and will default to false.
const animation = _.animation(callback, options);The returned animation function has a cancel method which will cancel the callback.
animation.cancel();Compose
Create a wrapped function that will execute each callback in reverse order, passing the result from each function to the previous.
Any arguments supplied will be added to the chain of callbacks.
const composed = _.compose(...callbacks);Curry
Create a wrapped version of a function, that will return new functions until the number of total arguments passed reaches the arguments length of the original function (at which point the function will execute).
callbackis the function you wish to wrap.
const curried = _.curry(callback);Debounce
Create a wrapped version of a function that executes once per wait period (using the most recent arguments passed to it).
callbackis the function you wish to wrap.waitis the number of milliseconds to wait between executions, and will default to 0.optionsis an object containing options for executing the function.leadingis a boolean indicating whether you wish the function to execute on the leading edge of the wait period, and will default to false.trailingis a boolean indicating whether you wish the function to execute on the trailing edge of the wait period, and will default to true.
const debounced = _.debounce(callback, wait, options);The returned debounced function has a cancel method which will cancel the (trailing) callback.
debounced.cancel();Evaluate
Evaluate a value from a function or value.
valueis the value to evaluate.
const result = _.evaluate(value);Once
Create a wrapped version of a function that will only ever execute once. Subsequent calls to the wrapped function will return the result of the initial call.
callbackis the function you wish to wrap.
const once = _.once(callback);Partial
Create a wrapped version of a function with predefined arguments.
callbackis the function you wish to wrap.
Any additional arguments supplied will be passed on as default arguments to the wrapped function. Specify undefined for a default argument to allow that argument to be sent to the wrapped function.
const partial = _.partial(callback, ...defaultArgs);Pipe
Create a wrapped function that will execute each callback in order, passing the result from each function to the next.
Any arguments supplied will be added to the chain of callbacks.
const piped = _.pipe(...callbacks);Throttle
Create a wrapped version of a function that executes at most once per wait period (using the most recent arguments passed to it).
callbackis the function you wish to wrap.waitis the number of milliseconds to wait between executions, and will default to 0.optionsis an object containing options for executing the function.leadingis a boolean indicating whether you wish the function to execute on the leading edge of the wait period, and will default to true.trailingis a boolean indicating whether you wish the function to execute on the trailing edge of the wait period, and will default to true.
const throttled = _.throttle(callback, wait, options);The returned throttled function has a cancel method which will cancel the (trailing) callback.
throttled.cancel();Times
Execute a function a specified number of times.
callbackis the function you wish to execute.amountis the number of times you wish the function to execute.
_.times(callback, amount);Math
Clamp
Clamp a value between a min and max.
valueis the number you wish to clamp.minis the number which will be the minimum of the clamped value, and will default to 0.maxis the number which will be the maximum of the clamped value, and will default to 1.
const clamp = _.clamp(value, min, max);Clamp Percent
Clamp a value between 0 and 100.
valueis the number you wish to clamp.
const clampPercent = _.clampPercent(value);Distance
Get the distance between two vectors.
x1is the number to be used as the first X co-ordinate.y1is the number to be used as the first Y co-ordinate.x2is the number to be used as the second X co-ordinate.y2is the number to be used as the second Y co-ordinate.
const dist = _.dist(x1, y1, x2, y2);Inverse Linear Interpolation
Inverse linear interpolation from one value to another.
minis the number to be used as the minimum of the "lerped" amount.maxis the number to be used as the maximum of the "lerped" amount.valueis the value to inverse interpolate.
const lerp = _.inverseLerp(min, max, value);Length
Get the length of an X,Y vector.
xis the number to be used as the X co-ordinate.yis the number to be used as the Y co-ordinate.
const len = _.len(x, y);Linear Interpolation
Linear interpolation from one value to another.
minis the number to be used as the minimum of the "lerped" amount.maxis the number to be used as the maximum of the "lerped" amount.amountis the amount to interpolate (between 0 and 1).
const lerp = _.lerp(min, max, amount);Map
Map a value from one range to another.
valueis the number you wish to map.fromMinis the number to be used as the minimum value of the current range.fromMaxis the number to be used as the maximum value of the current range.toMinis the number to be used as the minimum value of the target range.toMaxis the number to be used as the maximum value of the target range.
const map = _.map(value, fromMin, fromMax, toMin, toMax);Random
Return a random floating-point number.
ais the number to be used as the minimum value (inclusive).bis the number to be used as the maximum value (exclusive).
If b is omitted, this function will return a random number between 0 (inclusive) and a (exclusive).
If both arguments are omitted, this function will return a random number between 0 (inclusive) and 1 (exclusive).
const random = _.random(a, b);To Step
Round a number to a specified step-size.
valueis the number you wish to constrain to a specified "step".stepis the number to be used as the minimum step-size.
const toStep = _.toStep(value, step);Objects
Extend
Merge the values from one or more objects onto an object (recursively).
objectis the object you are merging to.
Any additional arguments supplied will be merged onto the first object.
_.extend(object, ...objects);Flatten
Flatten an object using dot notation.
objectis the object you are flattening.
const flattened = _.flatten(object);Forget Dot
Remove a specified key from an object using dot notation.
objectis the object you wish to remove a key from.keyis a string using dot notation, indicating the key to remove.
_.forgetDot(object, key);Get Dot
Retrieve the value of a specified key from an object using dot notation.
objectis the object you wish to retrieve a value from.keyis a string using dot notation, indicating the key to retrieve.defaultValueis the default value to return if the key does not exist, and will default to undefined.
const value = _.getDot(object, key, defaultValue);Has Dot
Returns true if a specified key exists in an object using dot notation.
objectis the object you wish to test for a key.keyis a string using dot notation, indicating the key to test for.
const hasKey = _.hasDot(object, key);Pluck Dot
Retrieve values of a specified key from an array of objects using dot notation.
objectsis the array of objects you wish to retrieve values from.keyis a string using dot notation, indicating the key to retrieve.defaultValueis the default value to return if the key does not exist, and will default to undefined.
const values = _.pluckDot(objects, key, defaultValue);Set Dot
Set a specified value of a key for an object using dot notation.
objectis the object you wish to set a value for.keyis a string using dot notation, indicating the key to set the value.valueis the value you wish to set the key to.optionsis an object containing options for setting the value.overwriteis a boolean indicating whether you wish to overwrite an existing key, and will default to true.
_.setDot(object, key, value, options);Strings
Camel Case
Convert a string to camelCase.
stringis the string you wish to transform to camelCase.
const camelCase = _.camelCase(string);Capitalize
Convert the first character of string to upper case and the remaining to lower case.
stringis the string you wish to capitalize.
const capitalized = _.capitalize(string);Escape
Convert HTML special characters in a string to their corresponding HTML entities.
stringis the string you wish to escape.
const escape = _.escape(string);Escape RegExp
Escape a string for use in RegExp.
stringis the string you wish to escape.
const escapeRegExp = _.escapeRegExp(string);Humanize
Convert a string to a humanized form.
stringis the string you wish to humanize.
const humanized = _.humanize(string);Kebab Case
Convert a string to kebab-case.
stringis the string you wish to transform to kebab-case.
const kebabCase = _.kebabCase(string);Pascal Case
Convert a string to PascalCase.
stringis the string you wish to transform to PascalCase.
const pascalCase = _.pascalCase(string);Random String
Return a random string.
lengthis the length of the random string, and will default to 16.charactersis a sequence of characters to generate the string from, and will default to 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ0123456789'.
const randomString = _.randomString(length, characters);Snake Case
Convert a string to snake_case.
stringis the string you wish to transform to snake_case.
const snakeCase = _.snakeCase(string);Unescape
Convert HTML entities in a string to their corresponding characters.
stringis the string you wish to unescape.
const unescape = _.unescape(string);Testing
Is Array
Returns true is the value is an array.
valueis the value you wish to test.
const isArray = _.isArray(value);Is Array Like
Returns true is the value is array-like.
valueis the value you wish to test.
const isArrayLike = _.isArrayLike(value);Is Boolean
Returns true if the value is a boolean.
valueis the value you wish to test.
const isBoolean = _.isBoolean(value);Is Document
Returns true is the value is a Document.
valueis the value you wish to test.
const isDocument = _.isDocument(value);Is Element
Returns true is the value is a HTMLElement.
valueis the value you wish to test.
const isElement = _.isElement(value);Is Fragment
Returns true if the value is a DocumentFragment.
const isFragment = _.isFragment(value);Is Function
Returns true if the value is a function.
valueis the value you wish to test.
const isFunction = _.isFunction(value);Is NaN
Returns true if the value is NaN.
valueis the value you wish to test.
const isNaN = _.isNaN(value);Is Node
Returns true is the value is a Node.
valueis the value you wish to test.
const isNode = _.isNode(value);Is Null
Returns true if the value is null.
valueis the value you wish to test.
const isNull = _.isNull(value);Is Numeric
Returns true if the value is numeric.
valueis the value you wish to test.
const isNumeric = _.isNumeric(value);Is Object
Returns true if the value is an object.
valueis the value you wish to test.
const isObject = _.isObject(value);Is Plain Object
Returns true if the value is a plain object.
valueis the value you wish to test.
const isPlainObject = _.isPlainObject(value);Is Shadow
Returns true if the value is a ShadowRoot.
const isShadow = _.isShadow(value);Is Text
Returns true if the value is a text Node.
const isText = _.isText(value);Is String
Returns true if the value is a string.
valueis the value you wish to test.
const isString = _.isString(value);Is Undefined
Returns true if the value is undefined.
valueis the value you wish to test.
const isUndefined = _.isUndefined(value);Is Window
Returns true if the value is a Window.
valueis the value you wish to test.
const isWindow = _.isWindow(value);