https://github.com/flo-bit/js-utils
useful javascript methods
https://github.com/flo-bit/js-utils
javascript utilities
Last synced: about 1 year ago
JSON representation
useful javascript methods
- Host: GitHub
- URL: https://github.com/flo-bit/js-utils
- Owner: flo-bit
- Created: 2023-01-14T21:09:32.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-28T12:05:19.000Z (over 3 years ago)
- Last Synced: 2025-03-28T19:48:10.508Z (about 1 year ago)
- Topics: javascript, utilities
- Language: JavaScript
- Homepage: https://flo-bit.github.io/js-utils/utils.js
- Size: 47.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# js-utils
some useful javascript methods and classes
## Use
```js
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
```js
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)
```js
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
```js
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
```js
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
```js
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]]`
```js
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