https://github.com/codewithmark/dom
Fastest way to manipulate json array data
https://github.com/codewithmark/dom
Last synced: 8 days ago
JSON representation
Fastest way to manipulate json array data
- Host: GitHub
- URL: https://github.com/codewithmark/dom
- Owner: codewithmark
- Created: 2018-05-03T20:02:38.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2025-07-09T03:55:38.000Z (11 months ago)
- Last Synced: 2025-07-09T04:34:32.990Z (11 months ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dom.js ๐งฉ
A lightweight, chainable JavaScript class for managing object arrays โ like a tiny in-memory database with filtering, transforming, and localStorage support.
---
## ๐ฆ Features
- โ
Fully **chainable** API (`add`, `update`, `remove`, etc.)
- โ
Works with **arrays of objects**
- โ
Filter, find, sort, map, and transform data
- โ
Easily **save/load** from `localStorage`
- โ
Schema-based **data validation**
- โ
Useful for state management, frontend tools, or browser apps
---
## ๐งช Usage with Data, Functions & Output
### โ Add Records
```js
const users = new Dom();
users
.add({ id: 1, name: "Alice", role: "admin" })
.add([
{ id: 2, name: "Bob", role: "user" },
{ id: 3, name: "Charlie", role: "guest" }
]);
console.log(users.result());
```
๐ค Output:
```js
[
{ id: 1, name: "Alice", role: "admin" },
{ id: 2, name: "Bob", role: "user" },
{ id: 3, name: "Charlie", role: "guest" }
]
```
---
### โ๏ธ Update Record
```js
users.update({ role: "editor" }, { id: 2 });
console.log(users.find({ id: 2 }));
```
๐ค Output:
```js
{ id: 2, name: "Bob", role: "editor" }
```
---
### โ Remove Record
```js
users.remove({ role: "guest" });
console.log(users.result());
```
๐ค Output:
```js
[
{ id: 1, name: "Alice", role: "admin" },
{ id: 2, name: "Bob", role: "editor" }
]
```
---
### ๐ Find & Filter
```js
console.log(users.find({ name: "Alice" }));
console.log(users.filter({ role: "admin" }));
```
๐ค Output:
```js
{ id: 1, name: "Alice", role: "admin" }
[
{ id: 1, name: "Alice", role: "admin" }
]
```
---
### ๐ง Select Fields
```js
console.log(users.select(["name"]));
```
๐ค Output:
```js
[
{ name: "Alice" },
{ name: "Bob" }
]
```
---
### ๐ Sort by Name
```js
users
.add({ id: 4, name: "Zara", role: "admin" })
.sortBy("name");
console.log(users.result());
```
๐ค Output:
```js
[
{ id: 1, name: "Alice", role: "admin" },
{ id: 2, name: "Bob", role: "editor" },
{ id: 4, name: "Zara", role: "admin" }
]
```
---
### ๐ Transform with `map()`
```js
users.map(u => ({
...u,
email: `${u.name.toLowerCase()}@example.com`
}));
console.log(users.result());
```
๐ค Output:
```js
[
{ id: 1, name: "Alice", role: "admin", email: "alice@example.com" },
{ id: 2, name: "Bob", role: "editor", email: "bob@example.com" },
{ id: 4, name: "Zara", role: "admin", email: "zara@example.com" }
]
```
---
### ๐พ Save & Load from LocalStorage
```js
users.save("users");
const loaded = new Dom("users");
console.log(loaded.result());
```
๐ค Output:
```js
[
{ id: 1, name: "Alice", role: "admin", email: "alice@example.com" },
{ id: 2, name: "Bob", role: "editor", email: "bob@example.com" },
{ id: 4, name: "Zara", role: "admin", email: "zara@example.com" }
]
```
---
### ๐งน Clear All Data
```js
users.clear();
console.log(users.result());
```
๐ค Output:
```js
[]
```
---
### โ
Validate Schema
```js
users
.add({ id: "oops", name: null, role: 999 })
.validate({ id: "number", name: "string", role: "string" })
.log();
```
๐ค Output:
```js
[
{ id: 1, name: "Alice", role: "admin", email: "alice@example.com" },
{ id: 2, name: "Bob", role: "editor", email: "bob@example.com" },
{ id: 4, name: "Zara", role: "admin", email: "zara@example.com" }
]
```
---
## ๐ License
MIT โ Free for personal or commercial use.