{"id":17218635,"url":"https://github.com/jwulf/immutable-global-store","last_synced_at":"2026-01-26T08:33:17.132Z","repository":{"id":138431999,"uuid":"242744566","full_name":"jwulf/immutable-global-store","owner":"jwulf","description":"An immutable global store demo for browser JS","archived":false,"fork":false,"pushed_at":"2023-08-03T06:20:21.000Z","size":296,"stargazers_count":0,"open_issues_count":6,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-29T10:41:55.197Z","etag":null,"topics":["article","browser-js","global","immutable","refactor"],"latest_commit_sha":null,"homepage":"https://www.joshwulf.com/blog/2020/02/avoid-global-state/","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/jwulf.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-24T13:33:57.000Z","updated_at":"2020-02-25T23:08:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"0bc60d2c-19fa-4af9-8930-2349ebc8655a","html_url":"https://github.com/jwulf/immutable-global-store","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jwulf/immutable-global-store","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwulf%2Fimmutable-global-store","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwulf%2Fimmutable-global-store/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwulf%2Fimmutable-global-store/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwulf%2Fimmutable-global-store/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwulf","download_url":"https://codeload.github.com/jwulf/immutable-global-store/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwulf%2Fimmutable-global-store/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28770833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-26T07:45:00.504Z","status":"ssl_error","status_checked_at":"2026-01-26T07:45:00.070Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["article","browser-js","global","immutable","refactor"],"created_at":"2024-10-15T03:47:38.135Z","updated_at":"2026-01-26T08:33:17.117Z","avatar_url":"https://github.com/jwulf.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Avoiding global mutable state in browser JS\n\n![Node.js CI](https://github.com/jwulf/immutable-global-store/workflows/Node.js%20CI/badge.svg)\n\nThis is the accompanying repo for the article \"[Avoiding global mutable state in browser JS](https://www.joshwulf.com/blog/2020/02/avoid-global-state/)\", demonstrating an immutable global store that encapsulates state and business validation rules in [40 lines of code](test.spec.js).\n\nThe article is part of my [StackOverflowed series](https://www.joshwulf.com/categories/stackoverflowed/), where I refactor code from StackOverflow questions, with commentary on what I'm doing and why.\n\nThe original code that I refactored this from was:\n\n```\nvar memArray = []\n```\n\nI converted it to 40 lines of code that encapsulates the entire expected usage of this array in the application with an API, and 125 lines of unit tests that prove its functionality.\n\n## To test\n\n* Clone this git repo\n* Install deps with: `pnpm i` or `npm i`\n* Run tests with: `jest`\n\n## The code\n\nHere is the entire implementation:\n\n```\nconst GlobalMemberStore = (() =\u003e {\n  let _members = []\n  const needsArg = arg =\u003e {\n    if (!arg) {\n      throw new Error (`Undefined passed as argument to Store!`)\n    }\n    return arg\n  }\n  const needsId = member =\u003e {\n    if (!member.id) {\n      throw new Error (`Undefined id on member passed as argument to Store!`)\n    }\n    return member\n  }\n  const Store = {\n    setMembers: members =\u003e (_members = members.map(m =\u003e ({...m}))),\n    getMembers: () =\u003e _members.map(m =\u003e ({...m})),\n    getMember: id =\u003e {\n      const member = _members.filter(m =\u003e m.id === id)\n      return member.length === 1 ? \n        { found: true, member: {...member[0]}} :\n        { found: false, member: undefined }\n    },\n    putMember: member =\u003e {\n      const m = needsId(needsArg(member))\n      if (Store.getMember(m.id).found) {\n        throw new Error(`${m.id} already exists!`)\n      }\n      _members = [..._members, {...m}]\n    },\n    updateMember: update =\u003e {\n      const u = needsId(needsArg(update))\n      if (!Store.getMember(u.id).found) {\n        throw new Error(`${u.id} does not exists!`)\n      }\n      _members = _members.map(m =\u003e m.id === u.id ? \n                              {...update} : m)\n    }\n  }\n  return Object.freeze(Store)\n})()\n```\n\nSee the article \"[Avoiding global mutable state in browser JS](https://www.joshwulf.com/blog/2020/02/avoid-global-state/)\" for a guided tour through the creation of this implementation, with an explanation of each step.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwulf%2Fimmutable-global-store","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwulf%2Fimmutable-global-store","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwulf%2Fimmutable-global-store/lists"}