{"id":17036125,"url":"https://github.com/kormanowsky/js-bit-list","last_synced_at":"2026-04-28T21:04:06.973Z","repository":{"id":86846264,"uuid":"282744821","full_name":"kormanowsky/js-bit-list","owner":"kormanowsky","description":"Store boolean values in a number","archived":false,"fork":false,"pushed_at":"2020-11-12T19:32:14.000Z","size":29,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T18:57:34.423Z","etag":null,"topics":["binary-system","javascript","utility","utility-class"],"latest_commit_sha":null,"homepage":"","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/kormanowsky.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-26T22:37:24.000Z","updated_at":"2023-07-10T04:18:57.000Z","dependencies_parsed_at":"2023-03-13T19:50:14.184Z","dependency_job_id":null,"html_url":"https://github.com/kormanowsky/js-bit-list","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kormanowsky%2Fjs-bit-list","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kormanowsky%2Fjs-bit-list/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kormanowsky%2Fjs-bit-list/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kormanowsky%2Fjs-bit-list/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kormanowsky","download_url":"https://codeload.github.com/kormanowsky/js-bit-list/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245031345,"owners_count":20549913,"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":["binary-system","javascript","utility","utility-class"],"created_at":"2024-10-14T08:49:26.688Z","updated_at":"2026-04-28T21:04:01.952Z","avatar_url":"https://github.com/kormanowsky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JavaScirpt Bit List\n\n- Bit list is a data structure which may be used to store arrays of boolean values in a number. This method allows such arrays to be used in places where the data shortness is important, e. g. in query string parameters.\n- This data structure plays with the binary representation of the numbers. Each element (named \"bit\") is just a bit of the number. To set the nth bit to true means to add pow(2, n) to the number, to set it to false means to subtract pow(2, n) from the number.\n\n## Examples\n\n```javascript\n// Import the class\nconst BitList = require(\"js-bit-list\");\n// Create new bit list\nlet list = new BitList();\n// Add some values\nlist.setBit(3, 1);\nlist.setBit(4, 1);\n// Convert to number\nlet num = list.toNumber(); // Gives 2**3 + 2**4 = 24\n// Convert to array\nlet arr = list.toArray(); // Gives [0, 0, 0, 1, 1]\n// Predefined values\nlet predefinedList = new BitList([0, 0, 0, 1, 1, 0, 0]);\nlet predefinedListFromNumber = new BitList(31);\n```\n\n## Working with objects\n\nBit list may work with objects so it is convenient to use it when you need to configure something. Imagine you have a list of cities and want to configure, which of them are available for delivery.\n\n```javascript\n// Create a list of keys (your cities here)\nconst Cities = [\"Moscow\", \"London\", \"Paris\", \"Prague\"];\n// Tip: extend BitList to use your own keys\nconst CitiesBitList = BitList.useKeys(Cities);\n/**\n * v1.1.0- (not recommended)\n */\nclass CitiesBitList extends BitList {\n  setObject(object) {\n    return super.setObject(object, Cities);\n  }\n\n  toObject() {\n    return super.toObject(Cities);\n  }\n}\n/**\n * END v1.1.0-\n */\n// Then use your class for configuration\nlet citiesOfRussia = new CitiesBitList().setObject({ Moscow: true });\n// Convert to number\nlet citiesNumber = citiesOfRussia.toNumber();\n// ...\n// Later, convert it back to object\nlet citiesFromNumber = new CitiesBitList(citiesNumber).toObject();\n```\n\n## New in v1.3.0: React support\n\nThe new `useBitList()` hook may be used in React applications.\n\n```javascript\nimport BitList from \"js-bit-list\";\nimport { useBitList } from \"js-bit-list/hook\";\nconst MyBitList = BitList.useKeys([\"myKey\"]);\nconst [list, setList] = useBitList(MyBitList, { myKey: 1 });\n// Later\nconst listNumber = list.toNumber();\nlist.setObject({ myKey: 0 });\nsetList(list);\n```\n\n## New in v2.2.0: .get(), .set(), .keys(), .enabledkeys(), .disabledKeys() and static .checkKey() methods in customized class: \n```javascript\nconst BitList = require(\"js-bit-list\"); \nconst FruitsBitList = BitList.useKeys([\"apple\", \"banana\", \"orange\"])\n// .checkKey() \nFruitsBitList.checkKey(\"apple\") // -\u003e 0 \nFruitsBitList.checkKey(\"tomato\") // -\u003e Error \n// .get() and .set()\nconst fruitsList = new FruitsBitList();\nfruitsList.get(\"apple\") // -\u003e 0\nfruitsList.set(\"orange\", 1); \nfruitsList.get(\"orange\") // -\u003e 1\n// .enabledKeys(), .disabledKeys(), .keys()\nfruitsList.enabledKeys() // -\u003e [\"orange\"]\nfruitsList.disabledKeys() // -\u003e [\"apple\", \"banana\"]\nfruitsList.keys() // -\u003e [\"apple\", \"banana\", \"orange\"]\n```\n\n### See example.js for working example\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkormanowsky%2Fjs-bit-list","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkormanowsky%2Fjs-bit-list","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkormanowsky%2Fjs-bit-list/lists"}