{"id":15416262,"url":"https://github.com/herber/nation","last_synced_at":"2025-08-10T22:34:56.122Z","repository":{"id":57308209,"uuid":"126823530","full_name":"herber/nation","owner":"herber","description":"🌍💻 A minimalist, functional state management library.","archived":false,"fork":false,"pushed_at":"2018-03-30T19:33:14.000Z","size":58,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T19:07:58.971Z","etag":null,"topics":["functional","functional-programming","minimalist","state","state-machine"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/herber.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-26T12:12:30.000Z","updated_at":"2023-04-17T18:27:10.000Z","dependencies_parsed_at":"2022-09-07T04:51:32.919Z","dependency_job_id":null,"html_url":"https://github.com/herber/nation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herber%2Fnation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herber%2Fnation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herber%2Fnation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/herber%2Fnation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/herber","download_url":"https://codeload.github.com/herber/nation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240391789,"owners_count":19793951,"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":["functional","functional-programming","minimalist","state","state-machine"],"created_at":"2024-10-01T17:11:20.555Z","updated_at":"2025-03-01T23:30:52.440Z","avatar_url":"https://github.com/herber.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eNation\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e🌍💻 A minimalist, functional state management library.\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://travis-ci.org/herber/nation\"\u003e\n    \u003cimg src=\"https://travis-ci.org/herber/nation.svg?branch=master\" alt=\"Build Status\"\u003e\n  \u003c/a\u003e\n\n  \u003ca href=\"https://codecov.io/gh/herber/nation\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/herber/nation/branch/master/graph/badge.svg\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Features\n\n - __Functional__: Nation enforces functional programming.\n - __Minimal__: Nation is a fully functional state management lib weighing less than 500 byte.\n - __Easy__: Nation's API is as minimal as possible.\n - __Immutable__: State can only be set in actions or using `setState`.\n - __Reactive__: The update event is emitted on every state change.\n\n## Philosophy\n\nState management libraries often weigh multiple kilobytes, even though most of the time you just need a small part of their API. __Nation's__ approach is different - nation only includes the bare minimum state management functionality.\n\nNation enforces functional programming, this allows nation to be even tinier and makes you code more explicit.\n\n## Install\n\n```\n$ npm install nation\n```\n\n## Usage\n\n```js\nconst nation = require('nation');\n\nconst ship = nation({\n  initial: () =\u003e ({\n    swimming: true\n  })\n});\n\nconsole.log(ship.state().swimming);\n// =\u003e true\n```\n\n#### Actions\n\n```js\nship.action(state =\u003e ({\n  setSwimming: (val) =\u003e {\n    state().swimming = val;\n  },\n  setName: (val) =\u003e {\n    state().name = val;\n  }\n}));\n\nship.actions().setName('merry');\nship.actions().setSwimming(false);\n\nconsole.log(ship.state().name);\n// =\u003e 'merry'\n\nconsole.log(ship.state().swimming);\n// =\u003e false\n```\n\n#### State can also be set using `setState`\n\n```js\nship.setState({\n  name: 'enterprise',\n  swimming: true\n})\n\nconsole.log(ship.state().name);\n// =\u003e 'enterprise'\n\nconsole.log(ship.state().swimming);\n// =\u003e true\n```\n\n#### Computed properties\n\nComputed properties can be accessed like normal state values. They are computed every time the state changes.\n\n```js\nship.computed(state =\u003e ({\n  swimmingName: () =\u003e {\n    return `The ${state().swimming} is ${state().name == true ? 'swimming' : 'not swimming'}.`;\n  }\n}));\n\nconsole.log(ship.state().swimmingName);\n// =\u003e 'The enterprise is swimming.'\n```\n\n#### Lifecycle\n\nNations lifecycle consists of a single method: `onChange`.\n\n```js\nship.onChange((state) =\u003e {\n  console.log('The state has changed - State: ', state);\n});\n```\n\n#### Async actions\n\nNation treats every function the same no matter if it's sync or async. The only difference is, that in async functions you have to use `setState` to set state.\n\n```js\nship.action(state =\u003e ({\n  setAsync: async () =\u003e {\n    ship.setState({ asyncValue: await asyncFn() })\n  }\n}));\n\nship.actions().setAsync();\n\n// Wait\n\nconsole.log(ship.state().asyncValue);\n```\n\n#### Nested state\n\nNation state can be nested, the `onChange` event will be passed on, so even nested state is reactive.\n\nNested state cannot be set using actions, you have to use `setState`.\n\n```js\nconst cargo = nation({\n  initial: () =\u003e {\n    loaded: true\n  }\n});\n\nship.setState({\n  cargo\n});\n\nship.onChange((state) =\u003e {\n  console.log('Loaded state: '+ state.loaded);\n})\n\ncargo.setState({\n  loaded: false\n});\n// =\u003e Loaded state: false\n```\n\n## License\n\nMIT © [Tobias Herber](http://tobihrbr.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fherber%2Fnation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fherber%2Fnation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fherber%2Fnation/lists"}