⚡ Tiny, simple, and robust technique for defining and acting with local states
Installations
npm install state-local
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
NPM Version
Statistics
22 Stars
42 Commits
3 Forks
4 Watching
2 Branches
3 Contributors
Updated on 06 Nov 2024
Bundle Size
3.00 kB
Minified
1.18 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
62,934,229
Last day
-3.2%
164,781
Compared to previous day
Last week
3.4%
869,150
Compared to previous week
Last month
18.2%
3,626,782
Compared to previous month
Last year
83.4%
33,341,309
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
State ·
:zap: Tiny, simple, and robust technique for defining and acting with local states (for all js environments - node, browser, etc.)
Synopsis
A local state for modules, functions, and other ECs
Motivation
We all love functional programming and the concepts of it. It gives us many clean patterns, which we use in our code regardless of exactly which paradigm is in the base of our codebase. But sometimes, for some reason, we can't keep our code "clean" and have to interact with items that are outside of the current lexical environment
For example:
:x:
1let x = 0; 2let y = 1; 3 4// ... 5function someFn() { 6 // ... 7 x++; 8} 9 10// ... 11function anotherFn() { 12 // ... 13 y = 6; 14 console.log(x); 15} 16 17// ... 18function yetAnotherFn() { 19 // ... 20 y = x + 4; 21 x = null; // 🚶 22}
The example above lacks control over the mutations and consumption, which can lead to unpredictable and unwanted results. It is just an example of real-life usage and there are many similar cases that belong to the same class of the problem
The purpose of this library is to give an opportunity to work with local states in a clear, predictable, trackable, and strict way
:white_check_mark:
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ x: 0, y: 1 }); 4 5// ... 6function someFn() { 7 // ... 8 setState(state => ({ x: state.x + 1 })); 9} 10 11// ... 12function anotherFn() { 13 // ... 14 setState({ y: 6 }); 15 const state = getState(); 16 console.log(state); 17} 18 19// ... 20function yetAnotherFn() { 21 // ... 22 setState(state => ({ y: state.x + 4, x: null })); 23}
We also can track the changes in items:
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ x: 0, y: 1 }, { 4 x: latestX => console.log('(⌐▀ ̯ʖ▀) Houston we have a problem; "x" has been changed. "x" now is:', latestX), 5 y: latestY => console.log('(⌐▀ ̯ʖ▀) Houston we have a problem; "y" has been changed. "y" now is:', latestY), 6}); 7 8// ...
We can use the subset of the state in some execution contexts:
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ x: 5, y: 7 }); 4 5// ... 6function someFn() { 7 const state = getState(({ x }) => ({ x })); 8 9 console.log(state.x); // 5 10 console.log(state.y); // ❌ undefined - there is no y 11}
And much more...
Documentation
Contents
Installation
You can install this library as an npm package or download it from the CDN and use it in node or browser:
1npm install state-local
or
1yarn add state-local
or
1<script src="https://unpkg.com/state-local/dist/state-local.js"></script> 2 3<script> 4// now it is available in `window` (window.state) 5const [getState, setState] = state.create({ x: 11, y: 13 }); 6// ... 7</script>
create
The default export has a method called create
, which is supposed to be a function to create a state:
1import state from 'state-local'; 2 3// state.create 4 5// ...
create
is a function with two parameters:
initial state
(required)handler
(optional)
initial state
initial state
is a base structure and a value for the state. It should be a non-empty object
1import state from 'state-local'; 2 3/* 4const [getState, setState] = state.create(); // ❌ error - initial state is required 5const [getState, setState] = state.create(5); // ❌ error - initial state should be an object 6const [getState, setState] = state.create({}); // ❌ error - initial state shouldn\'t be an empty object 7*/ 8 9const [getState, setState] = state.create({ isLoading: false, payload: null }); // ✅ 10// ...
handler
handler
is a second parameter for create
function and it is optional. It is going to be a handler for state updates. Hence it can be either a function or an object.
- If the handler is a function than it should be called immediately after every state update (with the latest state)
- If the handler is an object than the keys of that object should be a subset of the state and the values should be called immediately after every update of the corresponding field in the state (with the latest value of the field)
see example below:
if handler
is a function
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ x: 2, y: 3, z: 5 }, handleStateUpdate /* will be called immediately after every state update */); 4 5function handleStateUpdate(latestState) { 6 console.log('hey state has been updated; the new state is:', latestState); // { x: 7, y: 11, z: 13 } 7} 8 9setState({ x: 7, y: 11, z: 13 }); 10// ...
if handler
is an object
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ x: 2, y: 3, z: 5 }, { 4 x: handleXUpdate, // will be called immediately after every "x" update 5 y: handleYUpdate, // will be called immediately after every "y" update 6 // and we don't want to listen "z" updates 😔 7}); 8 9function handleXUpdate(latestX) { 10 console.log('(⌐▀ ̯ʖ▀) Houston we have a problem; "x" has been changed. "x" now is:', latestX); // ... "x" now is 7 11} 12 13function handleYUpdate(latestY) { 14 console.log('(⌐▀ ̯ʖ▀) Houston we have a problem; "y" has been changed. "y" now is:', latestY); // ... "y" now is 11 15} 16 17setState({ x: 7, y: 11, z: 13 }); 18// ...
getState
getState
is the first element of the pair returned by create
function. It will return the current state or the subset of the current state depending on how it was called. It has an optional parameter selector
1import state from "state-local"; 2 3const [getState, setState] = state.create({ p1: 509, p2: 521 }); 4 5const state = getState(); 6console.log(state.p1); // 509 7console.log(state.p2); // 521 8 9// or 10 11const { p1, p2 } = getState(); 12console.log(p1); // 509 13console.log(p2); // 521
selector
selector
is a function that is supposed to be passed (optional) as an argument to getState
. It receives the current state and returns a subset of the state
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ p1: 389, p2: 397, p3: 401 }); 4 5function someFn() { 6 const state = getState(({ p1, p2 }) => ({ p1, p2 })); 7 console.log(state.p1); // 389 8 console.log(state.p2); // 397 9 console.log(state.p3); // ❌ undefined - there is no p3 10}
setState
setState
is the second element of the pair returned by create
function. It is going to receive an object as a change for the state. The change object will be shallow merged with the current state and the result will be the next state
NOTE: the change object can't contain a field that is not specified in the "initial" state
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ x:0, y: 0 }); 4 5setState({ z: 'some value' }); // ❌ error - it seams you want to change a field in the state which is not specified in the "initial" state 6 7setState({ x: 11 }); // ✅ ok 8setState({ y: 1 }); // ✅ ok 9setState({ x: -11, y: 11 }); // ✅ ok
setState
also can receive a function which will be called with the current state and it is supposed to return the change object
1import state from 'state-local'; 2 3const [getState, setState] = state.create({ x:0, y: 0 }); 4 5setState(state => ({ x: state.x + 2 })); // ✅ ok 6setState(state => ({ x: state.x - 11, y: state.y + 11 })); // ✅ ok 7 8setState(state => ({ z: 'some value' })); // ❌ error - it seams you want to change a field in the state which is not specified in the "initial" state
License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 2/27 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 6 are checked with a SAST tool
Reason
34 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-44c6-4v22-4mhx
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.7
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More