[READ-ONLY] Mirror of https://github.com/flo-bit/js-utils. useful javascript methods flo-bit.github.io/js-utils/utils.js
javascript utilities
0

Configure Feed

Select the types of activity you want to include in your feed.

JavaScript 100.0%
32 1 0

Clone this repository

https://git.vm.fail/flo-bit.dev/js-utils https://git.vm.fail/did:plc:wdoiupckrk3y4efwn6y6sh2a
ssh://git@knot1.tangled.sh:2222/flo-bit.dev/js-utils ssh://git@knot1.tangled.sh:2222/did:plc:wdoiupckrk3y4efwn6y6sh2a

For self-hosted knots, clone URLs may differ based on your setup.


Readme.md

js-utils#

some useful javascript methods and classes

Use#

import Utils from "https://flo-bit.github.io/js-utils/utils.js";
import Vector from "https://flo-bit.github.io/js-utils/vector.js";

Methods#

Utils#

Utils.firstDefined#

return the first defined value

Utils.firstDefined(undefined, 2, 3); // 2
Utils.firstDefined(undefined, undefined, 3); // 3
Utils.firstDefined(undefined, undefined, undefined); // undefined

Utils.merge#

merge multiple objects with priority to earlier objects (works recursively and also deep clones)

Utils.merge({ a: 1 }, { b: 2 }); // { a: 1, b: 2 }
Utils.merge({ a: 1 }, { a: 2 }); // { a: 1 }
Utils.merge({ a: { b: 3 } }, { a: { b: 1, c: 2 } }); // { a: {b: 3, c:2} }

Utils.deepClone#

deep clone an object

const obj = { a: { b: 1 } };
const obj2 = Utils.deepClone(obj);
obj2.a.b = 2;
console.log(obj.a.b); // 1

Utils.resolve#

resolve all functions in an object

const obj = { a: () => 1 };
Utils.resolve(obj); // { a: 1 }

const obj2 = { a: (n) => n + 1, b: (n) => n * 2 };
Utils.resolve(obj2, 2); // { a: 3, b: 4 }

Utils.combine#

Utils.combine(a, b, (key = "preset"));

if a is not an object turn it into an object with a[key] = a return merge of a and b[a[key]]

Utils.combine("a", { a: { b: 3 } }); // { preset: "a", b: 3 }
Utils.combine({ preset: "a", b: 3, c: 5 }, { a: { b: 10, c: 5 } }); // { preset: "a", b: 3, c: 5 }

Utils.loadScript#

Utils.number#

Vector#