{"id":16170424,"url":"https://github.com/corentinth/chisels","last_synced_at":"2025-06-13T21:03:39.026Z","repository":{"id":256010720,"uuid":"854103033","full_name":"CorentinTh/chisels","owner":"CorentinTh","description":"Opinionated collection of reusable JS/TS utilities.","archived":false,"fork":false,"pushed_at":"2025-03-13T18:13:12.000Z","size":108,"stargazers_count":2,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T19:28:55.216Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/CorentinTh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["CorentinTh"],"buy_me_a_coffee":"cthmsst"}},"created_at":"2024-09-08T12:27:18.000Z","updated_at":"2024-12-29T16:12:09.000Z","dependencies_parsed_at":"2024-10-27T19:19:44.004Z","dependency_job_id":"9e006b8b-b608-448b-adec-ea7e7150b5b5","html_url":"https://github.com/CorentinTh/chisels","commit_stats":null,"previous_names":["corentinth/chisels"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fchisels","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fchisels/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fchisels/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fchisels/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CorentinTh","download_url":"https://codeload.github.com/CorentinTh/chisels/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243955755,"owners_count":20374373,"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":[],"created_at":"2024-10-10T03:18:42.939Z","updated_at":"2025-06-13T21:03:39.014Z","avatar_url":"https://github.com/CorentinTh.png","language":"TypeScript","funding_links":["https://github.com/sponsors/CorentinTh","https://buymeacoffee.com/cthmsst"],"categories":[],"sub_categories":[],"readme":"# Chisels - JS/TS utilities and types\n\nOpinionated collection of reusable JS/TS utilities and types.\n\n* **Tree-shakable ES modules**: Import only what you need\n* **Fully tested**: Rigorous unit tests with comprehensive coverage\n* **Dependency free**: No runtime dependencies\n* **TypeScript support**: Written in TypeScript with full type definitions\n* **Lightweight**: Small bundle size\n* **Modern**: Written with modern JS/TS syntax\n* **Named arguments**: Functions with preferably named arguments\n\n## Installation\n\n```bash\n# Using npm\nnpm install @corentinth/chisels\n\n# Using yarn\nyarn add @corentinth/chisels\n\n# Using pnpm\npnpm add @corentinth/chisels\n```\n\n## Usage\n\n```ts\n// Using ES6 import\nimport { safely } from '@corentinth/chisels';\n\n// Using CommonJS require\nconst { safely } = require('@corentinth/chisels');\n\nconst [result, error] = safely(mayThrowError);\n\nconsole.log({ result, error });\n```\n\n## API\n\n\u003c!-- API-DOCS-START --\u003e\n\n### `safelySync`\n\nFunction - [See source](./src/safely.ts#L42)\n\nSafely executes a function and return a tuple with the result and an error if any.\n\n\n```typescript\nconst [result, error] = safelySync(myFunction);\n\nif (error) {\n console.error(error);\n}\n\nconsole.log(result);\n```\n\n### `safely`\n\nFunction - [See source](./src/safely.ts#L19)\n\nSafely executes an async function or promise and return a tuple with the result and an error if any.\n\n\n```typescript\nconst [result, error] = await safely(myFunction);\n\nif (error) {\n console.error(error);\n}\n\nconsole.log(result);\n```\n\n### `formatBytes`\n\nFunction - [See source](./src/bytes.ts#L18)\n\nFormats a number of bytes into a human-readable string.\n\n\n```typescript\nconst formatted = formatBytes({ bytes: 4194304 });\n\nconsole.log(formatted); // 4 MiB\n```\n\n### `castError`\n\nFunction - [See source](./src/errors.ts#L17)\n\nCasts an unknown value to an Error.\n\n\n```typescript\n try {\n     // ...\n } catch (rawError) {\n   const error = castError(rawError);\n\n   // Do something with a proper Error instance\n }\n ```\n\n### `joinUrlPaths`\n\nFunction - [See source](./src/url.ts#L13)\n\nJoin URL parts and trim slashes.\n\n\n```typescript\nconst url = joinUrlPaths('/part1/', '/part2/', 'part3', 'part4/');\n\nconsole.log(url); // 'part1/part2/part3/part4'\n```\n\n### `buildUrl`\n\nFunction - [See source](./src/url.ts#L27)\n\nFunctional wrapper around URL constructor to build an URL string from a base URL and optional path, query params and hash.\n\n\n```typescript\nconst url = buildUrl({ baseUrl: 'https://example.com', path: 'foo', queryParams: { a: '1', b: '2' }, hash: 'hash' });\n\nconsole.log(url); // 'https://example.com/foo?a=1\u0026b=2#hash'\n```\n\n### `injectArguments`\n\nFunction - [See source](./src/injection.ts#L21)\n\nInjects arguments into a set of functions. Useful for DI of repositories, services, etc.\n\n\n```typescript\n const functions = {\n  getUser: ({ userId, db }) =\u003e db.users.find({ id: userId }),\n  removeUser: ({ userId, db }) =\u003e db.users.remove({ id: userId }),\n };\n\n const { getUser, removeUser } = injectArguments(functions, { db });\n\n getUser({ userId: 1 });\n removeUser({ userId: 1 });\n```\n\n### `memoizeOnce`\n\nFunction - [See source](./src/memo.ts#L13)\n\nThis function takes a function that returns a value and returns a new function that caches the result of the first call. Basically a argument-less memoization.\n\n\n```typescript\nconst getCwd = memoizeOnce(() =\u003e process.cwd());\n\n// process.cwd() is only called once\nconsole.log(getCwd());\nconsole.log(getCwd());\n```\n\n### `PartialBy`\n\nType alias - [See source](./src/types.ts#L17)\n\nMake some properties of T optional\n\n\n```typescript\ntype User = {\n  id: number;\n  name: string;\n  email: string;\n};\n\ntype PartialUser = PartialBy\u003cUser, 'email' | 'name'\u003e;\n\nconst user: PartialUser = { id: 1 };\n```\n\n### `Expand`\n\nType alias - [See source](./src/types.ts#L22)\n\nFlatten an object type for better IDE support\n\n### `Dictionary`\n\nType alias - [See source](./src/types.ts#L35)\n\nRecord\u003cstring, T\u003e alias\n\n\n```typescript\nconst dictionary: Dictionary\u003cnumber\u003e = {\n  a: 1,\n  b: 2,\n};\n```\n\n### `DeepPartial`\n\nType alias - [See source](./src/types.ts#L40)\n\nMake all properties of T optional recursively\n\n### `Subtract`\n\nType alias - [See source](./src/types.ts#L64)\n\nExclude properties of T that are in U\n\n\n```typescript\ntype User = {\n  id: number;\n  name: string;\n  email: string;\n};\n\ntype WithId = {\n  id: number;\n}\n\ntype UserWithoutId = Subtract\u003cUser, WithId\u003e;\n```\n\n\u003c!-- API-DOCS-END --\u003e\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more information.\n\n## Credits and Acknowledgements\n\nThis project is crafted with ❤️ by [Corentin Thomasset](https://corentin.tech).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorentinth%2Fchisels","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorentinth%2Fchisels","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorentinth%2Fchisels/lists"}