{"id":20195280,"url":"https://github.com/sveltinio/ts-utils","last_synced_at":"2026-05-12T17:31:56.387Z","repository":{"id":143648356,"uuid":"610944941","full_name":"sveltinio/ts-utils","owner":"sveltinio","description":"A bunch of common utilities in pure Typescript to deal with primitives, strings, urls, objects, collections, dates etc.","archived":false,"fork":false,"pushed_at":"2023-12-06T01:43:45.000Z","size":618,"stargazers_count":0,"open_issues_count":7,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T19:49:24.035Z","etag":null,"topics":["javascript","neverthrow","typescript","utilities-library"],"latest_commit_sha":null,"homepage":"https://ts-utils.sveltin.io/","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/sveltinio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2023-03-07T19:52:18.000Z","updated_at":"2023-09-05T06:57:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"b523df01-94d9-43fe-959c-0311397ddfc6","html_url":"https://github.com/sveltinio/ts-utils","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sveltinio%2Fts-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sveltinio%2Fts-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sveltinio%2Fts-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sveltinio%2Fts-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sveltinio","download_url":"https://codeload.github.com/sveltinio/ts-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241629773,"owners_count":19993710,"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":["javascript","neverthrow","typescript","utilities-library"],"created_at":"2024-11-14T04:16:38.848Z","updated_at":"2026-05-12T17:31:51.366Z","avatar_url":"https://github.com/sveltinio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n    \u003ch1\u003e@sveltinio/ts-utils\u003c/h1\u003e\n    \u003ca href=\"https://www.npmjs.com/package/@sveltinio/ts-utils\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@sveltinio/ts-utils.svg?style=flat\" alt=\"sveltinio/ts-utils NPM version badge\" /\u003e\u003c/a\u003e\n    \u0026nbsp;\n    \u003cimg src=\"./coverage.svg\" alt=\"sveltinio/ts-utils code coverage\" /\u003e\n     \u0026nbsp;\n    \u003ca href=\"https://ts-utils.sveltin.io\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/api-tsdoc-a855f7\" alt=\"sveltinio/ts-utils documentation\" /\u003e\u003c/a\u003e\n    \u0026nbsp;\n    \u003ca href=\"https://github.com/sveltinio/ts-utils/blob/main/LICENSE\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/license-mit-blue?style=flat\u0026logo=none\" alt=\"license\" /\u003e\n    \u003c/a\u003e\n\u003c/div\u003e\n\nA bunch of common utilities in pure Typescript to deal with primitives, strings, urls, objects, collections, dates etc.\n\n\u003e The package has been created to make my life easier with the requirements of [Sveltin] based projects and its [packages]. Anyhow, Sveltin is definitely not mandatory and you could find something fitting your needs.\n\n## Table of contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Type-Safe Error Handling](#type-safe-error-handling)\n- [What's inside?](#whats-inside)\n  - [Is](#is)\n  - [Collections](#collections)\n  - [Colors](#colors)\n  - [Dates](#dates)\n  - [Objects](#objects)\n  - [Paths](#paths)\n  - [Strings](#strings)\n  - [Urls](#urls)\n\n## Installation\n\n```bash\nnpm install @sveltinio/ts-utils\n# or\npnpm add @sveltinio/ts-utils\n# or\nyarn add @sveltinio/ts-utils\n```\n\n## Usage\n\nThe package supports ES Module and Common JS formats.\n\n### ESM\n\n```javascript\nimport { isString, isRegExp } from '@sveltinio/ts-utils/is';\nimport { contains, uniq } from '@sveltinio/ts-utils/collections';\nimport { isEmpty, camelToKebab } from '@sveltinio/ts-utils';\n// ...\n```\n\n### CJS\n\n```javascript\nconst { hasProperty, hasPropertyValue } = require('@sveltinio/ts-utils/objects')\n```\n\n## Type-Safe Error Handling\n\nExcept the functions returning a `boolean` value, the others mainly return a `Result` type that represents either success (`Ok`) or failure (`Err`) from [neverthrow] for a [type-safe error handling].\n\n```typescript\n// from neverthrow docs\ntype Result\u003cT, E\u003e\n  = Ok\u003cT, E\u003e  // contains a success value of type T\n  | Err\u003cT, E\u003e // contains a failure value of type E\n```\n\nThe ways to access the return value from a `Result` type may vary based on the use-case and your programming style. Below some samples for quick reference:\n\n```javascript\nimport { toKebabCase } from '@sveltinio/ts-utils/strings';\n\ntoKebabCase('hello world').value;\n// =\u003e \"hello-world\"\n\nconst result1 = toKebabCase('hello world');\nif (result1.isOk()) {\n  console.log(result1.value); // =\u003e \"hello-world\"\n}\n\nconst result2 = toKebabCase('hello world').match(\n  (str) =\u003e str, // =\u003e \"hello-world\"\n  (err) =\u003e `${err.message}`\n);\nconsole.log(result2);\n\ntoKebabCase(2)\n  .map((str) =\u003e console.log(str))\n  .mapErr((err) =\u003e {\n    throw new Error(err.message); // =\u003e Error: [strings.toKebabCase] Expected string value as input\n  });\n```\n\n\u003e Refer to the [neverthrow documentation] for the full picture.\n\n## What's inside?\n\n### Is\n\nType guard utilities helping with [primitive] values and basic objects.\n\nRead the **[docs](https://ts-utils.sveltin.io/modules/is.html)**.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/is';\n```\n\n\u003cbr/\u003e\n\n| Name          | Description                                          |\n|:--------------|:-----------------------------------------------------|\n| isBool        | Checks if a given value is of type `boolean`         |\n| isNumber      | Checks if a given value is of type `number`          |\n| isBigInt      | Checks if a given value is of type `bigint`          |\n| isString      | Checks if a given value is of type `string`          |\n| isArray       | Checks if a given value is an array                  |\n| isObject      | Checks if a given value is an object                 |\n| isPlainObject | Checks if a given value is a plain JavaScript object |\n| isFunction    | Checks if a given value is of type `function`        |\n| isSymbol      | Checks if a given value is of type `symbol`          |\n| isDate        | Checks if a given value is a valid `Date` object     |\n| isRegExp      | Checks if a given value is a valid `RegExp` object   |\n| isDefined     | Checks if a given value is defined and not null      |\n| isTruthy      | Checks if a given value is a [truthy] value          |\n| isNullish     | Checks if a value is null or undefined               |\n| isNull        | Checks if a given value is null                      |\n| isUndefined   | Checks if a given value is undefined                 |\n| isEmpty       | Checks if a given value is empty or not              |\n\n### Collections\n\nSome utilities to deal with arrays.\n\nRead the **[docs](https://ts-utils.sveltin.io/modules/collections.html)**.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/collections';\n```\n\n\u003cbr/\u003e\n\n| Name              | Description                                                                                         |\n|:----------------- |:--------------------------------------------------------------------------------------------------- |\n| sortBy            | Sorts an array of objects based on a specified property and order                                   |\n| groupedByOne      | (1:1) Groups an array of objects by a specified property and returns an array of grouped objects    |\n| groupedByMany     | (1:many) Groups an array of objects by a specified property and returns an array of grouped objects |\n| pickRandom        | Picks random values from an array of numbers or strings                                             |\n| shuffle           | Shuffles the elements of an array of number, or string values randomly                              |\n| shuffleByProperty | Shuffles an array of plain JavaScript objects by the specified property                             |\n| contains          | Checks if an array contains a given value or an array of values                                     |\n| uniq              | Removes duplicates from an array of numbers or strings                                              |\n\n### Colors\n\nSome utilities to deal with hex color strings.\n\nRead the **[docs](https://ts-utils.sveltin.io/colors/is.html)**.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/colors';\n```\n\n\u003cbr/\u003e\n\n| Name           | Description                                                                 |\n|:-------------- |:--------------------------------------------------------------------------- |\n| isHex          | Checks if a given string is a valid hexadecimal color code                  |\n| getHexValue    | Returns either the substring after the first character (if the string is a valid hex value) or an error message |\n| randomHexColor | Returns a string representing a random hex color                            |\n\n### Dates\n\nSome utilities to deal with javascript `Date`objects.\n\nRead the **[docs](https://ts-utils.sveltin.io/modules/dates.html)**.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/dates';\n```\n\n\u003cbr/\u003e\n\n| Name          | Description                                                                |\n|:------------- |:-------------------------------------------------------------------------- |\n| padTo2Digits  | Given a number, returns a string that is the number padded to two digits   |\n| dayOfMonth    | Given a date string in the format MM/DD/YYYY, returns the day of the month |\n| formatDate    | Given a date object, returns a string in the format DD/MM/YYYY             |\n| formatDateISO | Given a date object, returns a string in the format YYYY-MM-DD             |\n| monthShort    | Given a date string in the format MM/DD/YYYY, returns the short month name |\n\n### Objects\n\nSome utilities to deal with javascript objects, their properties and values.\n\nRead the **[docs](https://ts-utils.sveltin.io/modules/objects.html)**.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/objects';\n```\n\n\u003cbr/\u003e\n\n| Name                   | Description                                                                       |\n|:---------------------- |:--------------------------------------------------------------------------------- |\n| hasProperty            | Checks if a plain JavaScript object has a specified property                      |\n| hasProperties          | Checks if an object has all the specified properties                              |\n| hasPropertyValue       | Checks if an object has the specified property with the given value and same type |\n| hasPropertiesWithValue | Checks if an object has the specified properties with the given values            |\n| merge                  | Recursively merges two objects of compatible types                                |\n| getPropertyValue       | Gets the value of a property on an object                                         |\n| mapToCssVars           | Returns a CSS variable string from a plain object with key-value pairs            |\n\n### Paths\n\nSome utilities to deal with path strings. The functions do not interface with the real file system but they are used to extract some information from the given path string. It means not ensuring e.g. a file exists or a folder is a real folder on the disk. For that, simply use what already [exists](https://nodejs.org/api/path.html).\n\n\u003e Some look useless at first glance but they are not for Sveltin project requirements.\n\nRead the **[docs](https://ts-utils.sveltin.io/modules/paths.html)**.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/paths';\n```\n\n\u003cbr/\u003e\n\n| Name        | Description                                                                              |\n|:----------- |:---------------------------------------------------------------------------------------- |\n| isDir       | Checks if a given path is a directory                                                    |\n| isFile      | Checks if a given path is a file path                                                    |\n| dirname     | Returns the directory name from a given file path                                        |\n| filename    | Returns the filename from a given file path                                              |\n| isImage     | Checks if a given string is a valid image file name (jpg, jpeg, png, gif, webp, or avif) |\n| lastSegment | Returns the last segment of a given path string                                          |\n\n### Strings\n\nSome utilities to deal with strings and string conversions.\n\nRead the **[docs](https://ts-utils.sveltin.io/modules/strings.html)**.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/strings';\n```\n\n\u003cbr/\u003e\n\n| Name                 | Description                                                                           |\n|:-------------------- |:------------------------------------------------------------------------------------- |\n| normalize            | Normalizes a string by replacing non-word characters with a space character           |\n| capitalize           | Capitalizes first letter of the string and lowercase the rest                         |\n| capitalizeAll        | Capitalizes first letters of each word in a string                                    |\n| uppercase            | Converts a string to uppercase                                                        |\n| lowercase            | Converts a string to lowercase                                                        |\n| removeTrailingSlash  | Removes all trailing slashes from a string                                            |\n| textBetween          | Returns the substring between start and end delimiters                                |\n| toSlug               | Removes all non-word characters, and replaces all spaces with dashes                  |\n| toTitle              | Replaces all dashes with spaces, and capitalizes all words                            |\n| toSnakeCase          | Converts a string to snake_case format                                                |\n| toKebabCase          | Converts a string to kebab-case format                                                |\n| toCamelCase          | Converts a string to camelCase format                                                 |\n| toPascalCase         | Converts a string to PascalCase format                                                |\n| camelToSnake         | Converts a camelCase string to snake_case format                                       |\n| camelToKebab         | Converts a camelCase string to kebab-case format                                      |\n| isCommaSeparated     | Checks if a given string contains a comma-separated list                              |\n| toCommaSeparated     | Replaces all whitespace and semicolons with commas to return a comma-separated string |\n| removeFirstOccurence | Removes the first occurrence of a specified string from a given text.                 |\n\n### Urls\n\nSome utilities to deal with urls.\n\nRead the **[docs](https://ts-utils.sveltin.io/modules/urls.html)**.\n\n\u003e Some look useless at first glance but they are not for Sveltin project requirements.\n\n```javascript\nimport { ... } from '@sveltinio/ts-utils/urls';\n```\n\n\u003cbr/\u003e\n\n| Name           | Description                                                                       |\n|:-------------- |:--------------------------------------------------------------------------------- |\n| isUrl          | Checks if a given string is a valid URL                                           |\n| canonicalUrl   | Given a base URL and a pathname, returns a canonical URL                          |\n| makeImagePath  | Makes the full qualified path to the image file                                   |\n| parentUrl      | Given an URL, returns the parent url of it (w/o the last segment of the pathname) |\n| parentPathname | Given an URL, returns the parent pathname (w/o the last segment of the it)        |\n| pathSegments   | Extracts the path segments from a valid URL and returns them as an array          |\n\n## License\n\nFree and open-source software under the [MIT License](LICENSE)\n\n\u003c!-- resources --\u003e\n[Sveltin]: https://github.com/sveltinio/sveltin\n[packages]: https://github.com/sveltinio/components-library\n[neverthrow]: https://github.com/supermacro/neverthrow\n[type-safe error handling]: https://gdelgado.ca/type-safe-error-handling-in-typescript.html\n[neverthrow documentation]: https://github.com/supermacro/neverthrow#api-documentation\n[truthy]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy\n[primitive]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values\n[WAI ARIA Patterns]: https://www.w3.org/WAI/ARIA/apg/patterns/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsveltinio%2Fts-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsveltinio%2Fts-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsveltinio%2Fts-utils/lists"}