{"id":16359699,"url":"https://github.com/jmsv/oaty","last_synced_at":"2025-03-16T15:32:28.465Z","repository":{"id":34865399,"uuid":"184661355","full_name":"jmsv/oaty","owner":"jmsv","description":"Object Array Transposer(y) - JS objects with multiple key/value structures","archived":false,"fork":false,"pushed_at":"2024-06-12T01:11:49.000Z","size":304,"stargazers_count":10,"open_issues_count":10,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-12T02:09:29.956Z","etag":null,"topics":["array","hacktoberfest","javascript","oaty","object"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/oaty","language":"TypeScript","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/jmsv.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":"2019-05-02T22:29:23.000Z","updated_at":"2024-01-19T13:03:36.000Z","dependencies_parsed_at":"2023-12-10T16:28:57.441Z","dependency_job_id":"321ef847-091a-4717-ab19-7480b84d9024","html_url":"https://github.com/jmsv/oaty","commit_stats":{"total_commits":62,"total_committers":4,"mean_commits":15.5,"dds":0.6129032258064516,"last_synced_commit":"2b6acfd1d84877bf1550c3c9401ceee6e22adbc6"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmsv%2Foaty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmsv%2Foaty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmsv%2Foaty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmsv%2Foaty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmsv","download_url":"https://codeload.github.com/jmsv/oaty/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221665531,"owners_count":16860275,"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":["array","hacktoberfest","javascript","oaty","object"],"created_at":"2024-10-11T02:09:25.719Z","updated_at":"2024-10-27T10:51:09.070Z","avatar_url":"https://github.com/jmsv.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=320 src=\"https://repository-images.githubusercontent.com/184661355/24a79180-7409-11e9-8155-1e30fa6df50a\" alt=\"oaty\" /\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003cstrong\u003eObject Array Transposer(y)\u003c/strong\u003e - JS objects with multiple key/value structures\n\u003c/p\u003e\n\n## Why?\n\nThe idea ([j-m](https://github.com/j-m)'s) was to find a way of being able to get values from an array of objects in O(1) time, in an environment where memory isn't an issue.\n\nFor example, I could have the following JS object:\n\n```javascript\nconst food = [\n  {\n    id: 1,\n    name: \"apple\",\n    type: \"fruit\",\n  },\n  {\n    id: 2,\n    name: \"orange\",\n    type: \"fruit\",\n  },\n  {\n    id: 3,\n    name: \"broccoli\",\n    type: \"vegetable\",\n  },\n];\n```\n\nAs-is, to get the item where the id is 3, I'd use `food.find(x =\u003e x.id === 3)` or similar.\n\nIf the array was transposed to use `id` as object keys, the resultant object would look like the following:\n\n```javascript\nconst foodById = {\n  1: {\n    id: 1,\n    name: \"apple\",\n    type: \"fruit\",\n  },\n  2: {\n    id: 2,\n    name: \"orange\",\n    type: \"fruit\",\n  },\n  3: {\n    id: 3,\n    name: \"broccoli\",\n    type: \"vegetable\",\n  },\n};\n```\n\nThis way, we can get the food with the `id` of 3 with `foodById[3]`.\n\nHowever, we don't know that object keys will all be unique, so objects should be placed in arrays. For example:\n\n```javascript\nconst foodByType = {\n  fruit: [\n    {\n      id: 1,\n      name: \"apple\",\n      type: \"fruit\",\n    },\n    {\n      id: 2,\n      name: \"orange\",\n      type: \"fruit\",\n    },\n  ],\n  vegetable: [\n    {\n      id: 3,\n      name: \"broccoli\",\n      type: \"vegetable\",\n    },\n  ],\n};\n```\n\nNow, to get an array of fruit, rather than using `food.filter(f =\u003e f.type === 'fruit')` we can just use `foodByType['fruit']`.\n\n---\n\n\u003e **For data that changes frequently, this is a bad approach since transposing the data to use different key values is expensive.**\n\nHowever, for data that is assigned once (e.g. when a server first starts running) or assigned relatively infrequently (e.g. polling a database) this idea should be far less CPU-intensive than frequently searching the array using `filter`, `find`, or manually.\n\n## Getting Started\n\nThis library's default export is `OatyArray`. Initialise it as such:\n\n```javascript\nconst oatyFood = new OatyArray(food, { keys: [\"id\", \"type\"] });\n```\n\nIn the above case, `food` is the initial array of items and `['id', 'type']` is the array of keys you want to be able to query.\n\nThe `OatyArray` constructor generates the transposed caches.\n\nTo query data, use the `get` method:\n\n```javascript\nconst fruitArray = oatyFood.get(\"type\", \"fruit\");\n```\n\nThe above is effectively the same as `foodByType['fruit']` in the above examples\n\n## Development\n\n`oaty` is built with Typescript\n\nTo get started, run `npm install` to install dependencies.\n\n- Run tests: `npm test`\n- Build: `npm run build`\n\nFeel free to open Issues/PRs with suggestions/problems/improvements.\n\nLibrary is versioned as-per the [semver](https://semver.org) standard.\n\n### Maintainers\n\n- James Vickery - [jmsv](https://github.com/jmsv)\n- Jonathan Marsh - [j-m](https://github.com/j-m)\n\n### Changelog\n\n#### `1.0.0`\n\n- Added extra type inference and enforcement to Oaty, this means that an array can be initialised `new OatyArray([{ myKey: \"myValue\" }])` without needing to provide a type.\n- `keys` input is now enforced to a subset of the keys of the given type\n- `keys` input will now be used to enforce that only valid objects are transposed by Oaty\n- `.keys` will now always return either an array containing all the auto transposed keys, or an array of the configured keys, and is now type correct.\n- Oaty will now enforce the correct keys are the objects of `.push` meaning that an error will be thrown at compile time if an invalid object is being passed to Oaty.\n- Updated overloads on `.get` to return the correct types depending on usage\n- Updated to `typescript@3.8`\n- Added `tsd` tests\n\n#### `0.4.0`\n\n- Oaty can now be initialised with `new OatyArray\u003cT\u003e()` so that `.get` , `.data`, and `.transposed` return data of type `T`. This is completely optional as `T` defaults to `any`.\n- Removed `missingKeyReturns` and `noResultsReturns` options.\n- Exported a new type for the `_transposed` property.\n- Expanded the ternary conditions to improve readability.\n- Made the `data` constructor param optional and default to `[]`.\n- When a key is not transposed and `.get` searches for that key, it will now throw a `ReferenceError`.\n- Added NYC and increased tests to 100% coverage.\n\n#### `0.3.0`\n\n- Added `get`s for `data`, `keys`, and `transposed`\n- Made `keys` optional - will transpose every (root) key in an object\n- Added two options to change the return value for when they key or value of `.get()` is undefined\n- Renamed `original` to `data`\n- `.get()` can now retrieve all objects that have a key, like `.get('fruit')`\n- Added simple benchmark\n- Added more tests\n\n#### `0.2.1`\n\n- Fixed `.get()` method type (`object[]`)\n\n#### `0.2.0`\n\n- Renamed `OatyObject` to `OatyArray`, since it's intended as an array alternative, rather than an object alternative\n- Named export, rather than default export\n\n#### `0.1.0`\n\n- Added `.push` function\n\n#### `0.0.0`\n\n- Initial proof of concept\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmsv%2Foaty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmsv%2Foaty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmsv%2Foaty/lists"}