0.3.1 • Published 2 years ago

jabr v0.3.1

Weekly downloads
51
License
MIT
Repository
github
Last release
2 years ago

Jabr

No-nonsense State Management! No more boilerplate hell :)

Basics

Jabr is very simple, first we create a store object.

const {Jabr} = require('jabr')
const store = new Jabr()

This store object is used just like any other object in Javascript. We can read and modify properties without any special methods.

store.a = 12
console.log(store) // Logs 12

Event Listeners

Now the magic here is that we can attach listeners to our store. Whenever we modify the store our callback function is called with the new data. Let's look at an example:

store.on('a', value => {
	console.log(value)
})
store.a = 12 // Logs 12

Jabr triggers the callback function without requiring us to call any methods. It does this by using the Proxy API under the hood, but you don't need to worry about that unless your runtime doesn't support it (all modern Javascript Environments do).

Initializing the Store

We can intialize our store by supplying it with an object in the constructor.

const {Jabr} = require('jabr')
const store = new Jabr({b: "Long John Silvers"})

console.log(store.b) // Logs "Long John Silvers"

Configuring Property Options

You can pass additional options for each store property by passing a map of option objects for each property. There are a number of features you can supply, including data validation using Sandhands formats, normalization, and more

format

The format option can be used to ensure your data is properly formatted

const {Jabr} = require('jabr')
const store = new Jabr({age: 37}, {age: {format: Number}}) // Our store will now throw an error if we attempt to make age anything besides a number (it can still be deleted)

compute

Passing a function as the compute option will allow you to create a property whose value is computed on the fly.

const {Jabr} = require('jabr')
const store = new Jabr({}, {random: {compute: ()=>Math.random()}})
console.log(store.random) // Returns a new random value every time it is accessed

Properties that are computed on the fly cannot be assigned and do not trigger callbacks

default

When the default option is provided, if our property does not have a value currently and we access it the default value will be returned

Synchronizing store to JSON file

We can use the syncToJSON method in order to synchronously save our store to a JSON file. It will also read any existing values in the JSON file.

const {Jabr, syncToJSON} = require('jabr')
const store = new Jabr()
syncToJSON(store, 'data.json') // Loads any existing data in data.json (if it exists), overwriting any conflicting properties in the store
store.snack = "cookie" // Synchronously saves this data to the data.json file when we modify the store
0.3.0

2 years ago

0.2.9

2 years ago

0.3.1

2 years ago

0.2.8

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago