{"id":25120115,"url":"https://github.com/fabiopf02/groupby-js","last_synced_at":"2026-03-01T02:05:06.737Z","repository":{"id":37894351,"uuid":"458269403","full_name":"Fabiopf02/groupby-js","owner":"Fabiopf02","description":"Agrupador de array de objetos por propriedade","archived":false,"fork":false,"pushed_at":"2024-08-19T13:56:31.000Z","size":1015,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T10:35:46.290Z","etag":null,"topics":["array-manipulations","group-by","groupby","grouping","js","ts"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/groupby-js","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/Fabiopf02.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":"2022-02-11T16:55:14.000Z","updated_at":"2024-08-19T13:56:27.000Z","dependencies_parsed_at":"2024-08-19T16:11:08.554Z","dependency_job_id":"5c0f4301-eb2e-49f2-a15e-86879abdc927","html_url":"https://github.com/Fabiopf02/groupby-js","commit_stats":{"total_commits":34,"total_committers":3,"mean_commits":"11.333333333333334","dds":0.08823529411764708,"last_synced_commit":"6646c2d5134bea9b25d30ef07a1e74f0d2c0b369"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fabiopf02%2Fgroupby-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fabiopf02%2Fgroupby-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fabiopf02%2Fgroupby-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fabiopf02%2Fgroupby-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Fabiopf02","download_url":"https://codeload.github.com/Fabiopf02/groupby-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237760224,"owners_count":19361700,"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-manipulations","group-by","groupby","grouping","js","ts"],"created_at":"2025-02-08T05:19:15.897Z","updated_at":"2025-10-23T01:31:09.349Z","avatar_url":"https://github.com/Fabiopf02.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# groupby-js\n\n#### _Agrupador de array de objetos por propriedade_\n\n## Features\n\n- Agrupar em objetos com 'title' e 'items' -\u003e `\u003cT\u003e{ title: string, items: T[] }[] // Type = WithObjectType`\n- Agrupar em objetos `key: value` -\u003e `\u003cT\u003e{ key: T[] } // Type = ObjectWithKeysType`\n- Agrupar em  `Array` -\u003e `\u003cT\u003eT[][] // Type = WithArrayType`\n\n\n\n## Instalação\n\n```sh\nnpm i groupby-js\n```\n\n## Uso\n#### Estrutura da função\n```ts\n    // Type\n    import { groupBy, WithObjectType, WithArrayType, ObjectWithKeysType } from 'groupby-js'\n    groupBy\u003cT, WithObjectType | WithArrayType | ObjectWithKeysType\u003e(\n        /**\n        *   key -\u003e propriedade que será usada para agrupar o array\n        */\n        key: keyof T | string,\n        /**\n        *   O array que será agrupado\n        *   typescript -\u003e se o tipo T for fornecido, data = T[]\n        */\n        data: T[],\n        {\n            /*\n            *   Tipo de agrupamento\n            *   type='objects' return {title: string, items: T[]}[]\n            *   type='arrays' return T[][]\n            *   type='objectWithKeys' return {string: T[]}\n            *\n            *   default: type = 'objects'\n            */\n            type: 'objects' | 'arrays' | 'objectWithKeys',\n            /*\n            *   * Opcional *\n            *   Por padrão compara a igualdade (=) dos valores da propriedade 'key'\n            */\n            compare?: (obj, item: T, key: string = param key) =\u003e boolean,\n            /*\n            *   * Opcional *\n            *   Por padrão utiliza como 'título' o valor da propriedade 'key'\n            *   ** Funciona apenas para type = 'objects' e type = 'objectWithKeys' **\n            */\n            formatTitle?: (item: T, key: string = param key) =\u003e string,\n                // obj -\u003e elemento do array já agrupado\n                // item -\u003e elemento do array 'data' que será agrupado\n                // key -\u003e A mesma key passada para groupBy\n\n            /*\n            *   * Opcional *\n            *   Quantidade máxima de itens por grupo\n            *   -\u003e Deve ser um inteiro maior que zero\n            */\n            maxByGroup?: number\n        })\n```\n#### Typescript\nPara tipagem do retorno e _options_ (_`compare`_ e _`formatTitle`_) no typescript:\n```ts\n    groupBy\u003cT, K\u003e(key, data, options)\n    \n    //  T -\u003e Tipo dos objetos em data\n        type User = {\n            name: string\n            age: number\n        }\n        groupBy\u003cUser\u003e(key, [{ name: 'test', age: 20 }], options)\n    //  K -\u003e Deve ser fornecido de acordo com 'type' em 'options':\n    //       Para type='objects':\n                  import { WithObjectType } from 'groupby-js'\n                  groupBy\u003cT, WithObjectType\u003e(key, data, { type: 'objects' })\n    //       Para type='arrays':\n                  import { WithArrayType } from 'groupby-js'\n                  groupBy\u003cT, WithArrayType\u003e(key, data, { type: 'arrays' })\n    //       Para type='objectWithKeys':\n                  import { ObjectWithKeysType } from 'groupby-js'\n                  groupBy\u003cT, ObjectWithKeysType\u003e(key, data, { type: 'objectWithKeys' })\n\n```\n\n#### Para _`type='objects'`_\n```ts\nimport { groupBY } from 'groupby-js'\n// ou\nconst { groupBy } = require('groupby-js')\n\nconst data = [\n    { name: 'test1', group: 'g1' },\n    { name: 'test2', group: 'g2' },\n    { name: 'test3', group: 'g1' },\n]\n\n// javascript\nconst groups = groupBy('group', data, { type: 'objects', maxByGroup: 1 })\n\n// typescript\nimport { WithObjectType } from 'groupby-js'\n\ntype Data = {\n    name: string\n    group: string\n}\nconst groups = groupBy\u003cData, WithObjectType\u003e('group', data, { type: 'objects', maxByGroup: 1 })\n\nconsole.log(groups)\n// retorno\n[\n    {\n        title: 'g1',\n        items: [\n            { name: 'test1', group: 'g1' },\n        ],\n    },\n    {\n        title: 'g2',\n        items: [\n            { name: 'test2', group: 'g2' },\n        ],\n    },\n]\n```\n#### Para _`type='arrays'`_\n```ts\nimport { groupBY } from 'groupby-js'\n// ou\nconst { groupBy } = require('groupby-js')\n\nconst data = [\n    { name: 'test1', group: 'g1' },\n    { name: 'test2', group: 'g2' },\n    { name: 'test3', group: 'g1' },\n]\n\n// javascript\nconst groups = groupBy('group', data, { type: 'arrays', maxByGroup: 1 })\n\n// typescript\nimport { WithArrayType } from 'groupby-js'\n\ntype Data = {\n    name: string\n    group: string\n}\nconst groups = groupBy\u003cData, WithArrayType\u003e('group', data, { type: 'arrays', maxByGroup: 1 })\n\nconsole.log(groups)\n// retorno\n[\n     [\n        { name: 'test1', group: 'g1' },\n    ],\n    [\n        { name: 'test2', group: 'g2' },\n    ],\n]\n```\n#### Para _`type='objectWithKeys'`_\n```ts\nimport { groupBY } from 'groupby-js'\n// ou\nconst { groupBy } = require('groupby-js')\n\nconst data = [\n    { name: 'test1', group: 'g1' },\n    { name: 'test2', group: 'g2' },\n    { name: 'test3', group: 'g1' },\n]\n\n// javascript\nconst groups = groupBy('group', data, { type: 'objectWithKeys', maxByGroup: 1 })\n\n// typescript\nimport { ObjectWithKeysType } from 'groupby-js'\n\ntype Data = {\n    name: string\n    group: string\n}\nconst groups = groupBy\u003cData, ObjectWithKeysType\u003e('group', data, { type: 'objectWithKeys', maxByGroup: 1 })\n\nconsole.log(groups)\n// retorno\n[\n    'g1': [\n        { name: 'test1', group: 'g1' },\n    ],\n    'g2': [\n        { name: 'test2', group: 'g2' },\n    ],\n]\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiopf02%2Fgroupby-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabiopf02%2Fgroupby-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiopf02%2Fgroupby-js/lists"}