Creuna js utils
npm install @creuna/utils

A collection of useful JS utility functions.
You can choose whether you want to import everyting or individual modules.
``js
// Import everything:
import utils from "@creuna/utils";
// Import single:
import rangeMap from "@creuna/utils/range-map";
`
_@creuna/utils/any-to-kebab_
* input: string
* returns: string
`js`
anyToKebab("CamelCaseString"); // "camel-case-string"
_@creuna/utils/array-insert_
* array: arrayindex
* : numberitem
* : any
* returns: array
Returns a new array that is a deep clone of the one passed in containing the new item at the specified index.
`js`
arrayInsert(["a", "b", "c"], 1, "x"); // ['a', 'x', 'b', 'c']
_@creuna/utils/array-move_
* array: arrayoldIndex
* : numbernewIndex
* : number
* returns: array
Returns a new array that is a deep clone of the one passed in with the item at oldIndex moved to newIndex.
`js`
arrayInsert(["a", "b", "c"], 1, 2); // ['a', 'c', 'b']
_@creuna/utils/array-remove_
* array: arrayindex
* : number
* returns: array
Returns a new array that is a deep clone of the one passed in with the item at index removed.
`js`
arrayInsert(["a", "b", "c"], 1); // ['a', 'c']
_@creuna/utils/clamp_
* value: numberminimum
* : numbermaximum
* : number
* returns: number
clamp can be used to keep a value between a min and max value.
_@creuna/utils/create-pipe_
- functions: any number of functions
- returns: function
The pipe combines n functions, calling each function with the output of the last one. To actually execute the pipeline, call the returned function with a value. More on this concept here.
`js`
pipe(
removeSpaces,
capitalize,
reverseString
)("a b c"); // "CBA"
_@creuna/utils/deep-clone_
* thing: object | array
* returns: object | array
Returns a deep clone of an object (any nested objects or arrays will also be cloned). Be aware that this uses JSON.stringify, meaning that any array elements or object values that are undefined will be stripped
_@creuna/utils/filter-object_
- object: objectvalue
- : (key: string, value: any): boolean
- returns: object
Tests predicate (with key and value) for every entry in object. Returns a new object that contains every entry that passed the test.
`js`
const obj = { a: 1, b: 2, c: 3 };
filterObject(obj, (key, value) => key !== "b" && value !== 3); // { a: 1 }
_@creuna/utils/from-query-string_
* queryString: stringprefix
* : string (default '?')
* returns: object
Converts a query string into an object.
`js
fromQueryString("?param=test&other=test");
// { param: "test", other: "test" }
fromQueryString("#param=test&other=test", "#");
// { param: "test", other: "test" }
`
_@creuna/utils/get-data_
* dataAttributeName: string
* returns: string
Returns the value of the first DOM node with dataAttributeName
`html`
`js`
getData("some-attribute"); // "true"
_@creuna/utils/is-equal_
* a: anyb
* : any
* returns: boolean
Checks whether a and b are equal (deep comparison for objects and arrays). This uses JSON.stringify, so be aware that array elements or object values that are undefined will be stripped.
_@creuna/utils/is-fully-in-viewport_
* node: DOM node
* returns: boolean
Checks whether the given element is fully visible in the viewport. This is a special case of isInViewport where the offsets are the dimensions of the element.
_@creuna/utils/is-in-viewport_
* node: DOM nodeoffset
* : numberoffsetX
* : number (defaults to offset)
* returns: boolean
Checks whether the given element is visible in the viewport. Positive numbers for offset mean more of the element needs to be in the viewport while negative numbers mean that the element can be outside of the viewport.
_@creuna/utils/is-running-on-client_
* exports: bool
Exports a boolean indicating whether or not the code is running on the client.
`js`
import isRunningOnClient from "@creuna/utils/is-running-on-client"; // true || false
_@creuna/utils/kebab-to-camel_
* kebabString: string (kebab-case formatted)
* returns: string (camelCase formatted)
`js`
kebabToCamel("kebab-string"); // "kebabString"
_@creuna/utils/kebab-to-pascal_
* kebabString: string (kebab-case formatted)
* returns: string (PascalCase formatted)
`js`
kebabToPascal("kebab-string"); // "KebabString"
_@creuna/utils/pipe_
- value: any valuefunctions
- : any number of functionsvalue
- returns: any (the result of running through the pipeline)
A function that emulates the pipeline operator. For more advanced composition stuff, see createPipe.
`js`
pipeValue("a b c", removeSpaces, capitalize, reverseString); // "CBA"
_@creuna/utils/range-map_
* val: numberinMin
* : numberinMax
* : numberoutMin
* : numberoutMax
* : number
* returns: number
Re-maps a number from one numeric range onto another.
`js`
rangeMap(0.5, 0, 1, 0, 100); // 50
_@creuna/utils/replace-query-parameters_
* url: stringquery
* : object
* returns: string
Adds or replaces query parameters in url.
`js`
replaceQueryParameters("http://lorem.com?param1=a¶m2=b", {
param2: "BBB",
param3: "CCC"
});
// "http://lorem.com?param1=a¶m2=CCC¶m3=DDD"
_@creuna/utils/scroll-to-element_
* node: DOM nodeoffset
* : number (default 0 px)duration
* : number (default 500 ms)delay
* : number (default 0 ms)
Finds the scroll position of node and scrolls it into view (animated)
_@creuna/utils/scroll-to-position_
* y: numberduration
* : number (default 500 ms)
_@creuna/utils/scrolling-element_
Get the scrolling element (useful because some browsers use document.scrollingElement, some use document.documentElement and others use document.body. When not running on client, scrollingElement is undefined.
`js
import scrollingElement from "@creuna/utils/scrolling-element";
if (scrollingElement) {
scrollingElement.scrollTo(0, 100);
}
`
_@creuna/utils/strip-properties-with-keys_
- object: objectvalue
- : string[]
- returns: object
Returns a shallow copy of object with properties matching any of the keys removed
`js`
const obj = { a: 1, b: 2, c: 3 };
stripPropertiesWithKeys(obj, ["b", "c"]); // { a: 1 }
_@creuna/utils/strip-properties-with-value_
* object: objectvalue
* : any
* returns: object
Returns a shallow copy of object with properties matching value removed
`js`
const obj = { a: 1, b: 2 };
stripPropertiesWithValue(obj, 2); // { a: 1 }
_@creuna/utils/strip-undefined_
* object: object
* returns: object
Returns a shallow copy of object with properties matching undefined removed
_@creuna/utils/to-query-string_
* object: objectoptions
* : object || bool (bool is used for encode)encode
* : bool (default true)prefix
* : string (default '?')
* returns: string
Creates a query string representation of object. Encodes object values by default.
`js
toQueryString({ param: "test", other: "test" });
// "?param=test&other=test"
toQueryString({ param: "test foo", other: "test" });
// "?param=test%20foo&other=test"
toQueryString({ param: "test foo", other: "test" }, { encode: false });
// "?param=test foo&other=test"
toQueryString({ param: "test", other: "test" }, { prefix: "#" });
// "#param=test&other=test"
`
_@creuna/utils/try-parse-json_
* json: string (json)default
* : anydefault` if unsuccessful
* returns: object if sucessfully parsed;