{"id":16227376,"url":"https://github.com/malipetek/straightforward-state","last_synced_at":"2025-06-30T00:36:24.116Z","repository":{"id":42782588,"uuid":"472076995","full_name":"malipetek/straightforward-state","owner":"malipetek","description":"Straightforward State, with conditional watching at one level deep","archived":false,"fork":false,"pushed_at":"2022-03-28T08:02:03.000Z","size":17396,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-08T04:29:26.995Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/malipetek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-20T19:07:41.000Z","updated_at":"2023-03-08T03:22:34.000Z","dependencies_parsed_at":"2022-08-28T09:11:38.229Z","dependency_job_id":null,"html_url":"https://github.com/malipetek/straightforward-state","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/malipetek/straightforward-state","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malipetek%2Fstraightforward-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malipetek%2Fstraightforward-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malipetek%2Fstraightforward-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malipetek%2Fstraightforward-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/malipetek","download_url":"https://codeload.github.com/malipetek/straightforward-state/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malipetek%2Fstraightforward-state/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262689897,"owners_count":23349138,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T12:52:33.143Z","updated_at":"2025-06-30T00:36:24.090Z","avatar_url":"https://github.com/malipetek.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Straightforward State\n[![Percentage of issues still open](http://isitmaintained.com/badge/open/malipetek/straightforward-state.svg)](http://isitmaintained.com/project/malipetek/straightforward-state \"Percentage of issues still open\")\n[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/malipetek/straightforward-state.svg)](http://isitmaintained.com/project/malipetek/straightforward-state \"Average time to resolve an issue\")\n\n\n[![GitHub stars](https://img.shields.io/github/stars/malipetek/straightforward-state.svg?style=social\u0026label=Star\u0026maxAge=2592000)](https://GitHub.com/malipetek/straightforward-state/stargazers/)\n\n[![Npm package total downloads](https://badgen.net/npm/v/@malipetek/straightforward-state)](https://npmjs.com/package/v/@malipetek/straightforward-state)\n[![Npm package total downloads](https://badgen.net/npm/dt/@malipetek/straightforward-state)](https://npmjs.com/package/dt/@malipetek/straightforward-state)\n\n[![Npm package total downloads](https://badgen.net/packagephobia/publish/@malipetek/straightforward-state)](https://badgen.net/packagephobia/publish/@malipetek/straightforward-state)\n\n[![Npm package total downloads](https://badgen.net/npm/types/@malipetek/straightforward-state)](https://badgen.net/npm/types/@malipetek/straightforward-state)\n\n\nA state object that is conditionally watchable on first depth only.\n\n## Play\n### On codesandbox: \n[![Edit magical-wilbur-f247um](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/magical-wilbur-f247um?fontsize=14\u0026hidenavigation=1\u0026theme=dark)\n### On Stackblitz:\n[Edit on Stackblitz](https://stackblitz.com/edit/js-gc4vuk?file=index.js)\n## Install:\n\n```\nnpm i -s @malipetek/straigtforward-state\n```\n\nor\n\n```\nyarn add @malipetek/straigtforward-state\n```\n\n## import or require\n\n```js\nimport state from \"@malipetek/straigtforward-state\";\n```\n\nor\n\n```js\nconst state = require(\"@malipetek/straigtforward-state\");\n```\n\nAlternatively you can get instances like this:\n\n```js\nimport { newState } from \"@malipetek/straigtforward-state\";\nconst state1 = newState();\nconst state2 = newState();\n```\n## Initialize\n\n```js\nstate.set = { things: [1,2,3], numberOfThings: 6, aLotOfThings: false };\n```\n\nor\n\n\n```js\nstate.set({ things: [1,2,3], numberOfThings: 3, aLotOfThings: false });\n```\n\n## Access\n\nAccess values as usual\n\n```js\nstate.things // [1,2,3]\nstate.numberOfThings // 3\n```\n\n## Set\nSet the state properties as usual. When set watchers will be run, mutations does not trigger however, like pushing to an array value.\n```js\nstate.things = [...state.things, 4];\n```\n\n## Watch\n### Watch a member\n```js\nstate.watch.things = (val, was) =\u003e {\n  val // [1,2,3,4]\n  state.things // [1,2,3,4]\n\n  state.numberOfThings = state.things.length;\n};\n\nstate.watch.numberOfThings((val, was) =\u003e {\n  val // 4\n  was // 3\n});\n```\n### Watch All\n```js\nstate.watch.all = (changedKey, val, was) =\u003e {\n  changedKey // 'things', 'numberOfThings'\n  state[changedKey] // [1], 1\n  val // [1], 1\n  was // [1,2,3], 3\n};\n\nstate.things = [1];\nstate.numberOfThings = 1;\n```\n## Conditional watch\n```js\nstate.when.things.watch.numberOfThings(callback);\n// or\nconst newThings = [1,2];\nstate.when.things.is(newThings).watch.numberOfThings = (val, was) =\u003e {\n  val // === newThings\n};\nstate.things = newThings;\n```\n\n## *and*, *or* Operators\n```js\nstate.when.numberOfThings.or.aLotOfThings.watch.things((val, was) =\u003e {\n\n});\n\nstate.when.numberOfThings.and.aLotOfThings.watch.things = (val, was) =\u003e {\n  // fires once\n};\n\nstate.things = [1,2,3,4];\n\nstate.aLotOfThings = true;\n\nstate.things = [1,2,3,4];\n```\n\n## *is* operator\nIs operator can take a value or a function with 1 param that is *the state member*.\n```js\nstate.when.numberOfThings.is(v =\u003e v.length \u003e 6).and.aLotOfThings.is(false).watch.things((things) =\u003e {\n  state.aLotOfThings = true;\n});\n\nstate.things = [1,2,3,4,5,6,7,8];\n```\n\n## *more* and *less* operators\n*more* and *less* operators only can be used after is fn call followed by a *or*.\n```js\nstate.when.numberOfThings.is(6).or.more.watch.things(callback);\n```\n\n👉 [Submit feedback](mailto:malipetek@gmail.com?subject=About%20Straightforward%20State%20Package\u0026body=Hi%2C%20I%20checked%20this%20package%20and%20...)\n\n👉 [Submit issues](https://github.com/malipetek/straightforward-state/issues)\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalipetek%2Fstraightforward-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmalipetek%2Fstraightforward-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalipetek%2Fstraightforward-state/lists"}