https://github.com/shipengine/capitalization
String casing functions with special cases for certain ShipEngine words and phrases
https://github.com/shipengine/capitalization
Last synced: 3 months ago
JSON representation
String casing functions with special cases for certain ShipEngine words and phrases
- Host: GitHub
- URL: https://github.com/shipengine/capitalization
- Owner: ShipEngine
- License: apache-2.0
- Created: 2019-07-04T16:49:06.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-23T19:28:57.000Z (over 4 years ago)
- Last Synced: 2025-01-30T18:03:29.834Z (4 months ago)
- Language: TypeScript
- Size: 138 KB
- Stars: 1
- Watchers: 22
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ShipEngine Capitalization
==============================================
String capitalization functions with special cases for certain ShipEngine words and phrases[](https://github.com/ShipEngine/capitalization/actions)
[](https://github.com/ShipEngine/capitalization/actions)[](https://coveralls.io/github/ShipEngine/capitalization)
[](https://david-dm.org/ShipEngine/capitalization)[](https://www.npmjs.com/package/@shipengine/capitalization)
[](LICENSE)Example
--------------------------```javascript
import { snakeCase, pascalCase, titleCase } from "@shipengine/capitalization";snakeCase("TheShipEngineSDKForWoocommerce"); // --> the_shipengine_sdk_for_woo_commerce
pascalCase("the_shipengine_sdk_for_woocommerce"); // --> TheShipEngineSdkForWooCommerce
titleCase("theShipengineSDKForWoocommerce"); // --> The ShipEngine SDK for WooCommerce
```Installation
--------------------------
You can install ShipEngine Capitalization via [npm](https://docs.npmjs.com/about-npm/).```bash
npm install @shipengine/capitalization
```Usage
--------------------------
You can import the specific capitalization function(s) that you need to use:```javascript
import { kebabCase, camelCase, sentenceCase } from "@shipengine/capitalization";
```Or you can import the entire capitalization module and use its methods:
```javascript
import capitalization from "@shipengine/capitalization";capitalization.kebabCase("some text");
```API
--------------------------
ShipEngine Capitalization exports several different capitalization functions, each of which accepts the same parameters:- `text` - A string containing one or more words. The string can be in any format (snake case, camel case, pascal case, sentence case, etc.)
- `options` - An optional [`Options` object](#options-object)
### `snakeCase(text, [options])`
Snake case strings are all lowercase with underscores as word separators. No whitespace or punctuation characters are allowed, and the string must begin with a letter.|Example inputs |Example outputs
|:-----------------------------------|:----------------------------------------------
|`Hello, World!` |`hello_world`
|`TheShipEngineAPI` |`the_shipengine_api`### `kebabCase(text, [options])`
Kebab case strings are all lowercase with hyphens as word separators. No whitespace or punctuation characters are allowed, and the string must begin with a letter.|Example inputs |Example outputs
|:-----------------------------------|:----------------------------------------------
|`Hello, World!` |`hello-world`
|`TheShipEngineAPI` |`the-shipengine-api`### `camelCase(text, [options])`
Camel case strings use capitalization as word separators. Each new word begins with a capital letter, followed by lowercase letters. No whitespace or punctuation characters are allowed, and the string must begin with a lowercase letter.|Example inputs |Example outputs
|:-----------------------------------|:----------------------------------------------
|`Hello, World!` |`helloWorld`
|`this is the shipengine api` |`thisIsTheShipEngineApi`### `pascalCase(text, [options])`
Pascal case strings use capitalization as word separators. Each new word begins with a capital letter, followed by lowercase letters. No whitespace or punctuation characters are allowed, and the string must begin with an uppercase letter.|Example inputs |Example outputs
|:-----------------------------------|:----------------------------------------------
|`Hello, World!` |`HelloWorld`
|`this is the shipengine api` |`ThisIsTheShipEngineApi`### `sentenceCase(text, [options])`
Sentence case strings follow basic English sentence rules. Words are separated by spaces. The first word is capitalized, and most other words are lowercase, except for proper nouns and acronyms. Punctuation characters are allowed.|Example inputs |Example outputs
|:-----------------------------------|:----------------------------------------------
|`hello_world` |`Hello world`
|`this is the shipengine api` |`This is the ShipEngine API`### `titleCase(text, [options])`
Title case strings follow [title case rules](https://capitalizemytitle.com/). Words are separated by spaces, most words are capitalized, and punctuation characters are allowed.|Example inputs |Example outputs
|:-----------------------------------|:----------------------------------------------
|`hello_world` |`Hello World`
|`this is the shipengine api` |`This is the ShipEngine API`### `Options` object
All of the ShipEngine Capitalization function accept an optional second argument, which is an options object. This object can contain any of the following options:|Option |Type |Default |Description
|:------------------|:----------------|:--------------------|:--------------------------------------------
|`prefix` |`string` |n/a |A prefix to be used when the result would otherwise be invalid due to starting with an illegal character.
For example, `snakeCase("4x6 label")` would throw an error because snake case strings cannot start with a number. But `snakeCase("4x6 label", { prefix: "size" })` would return `size_4x6_label`.
**Note:** The prefix is only added when needed. So `snakeCase("label size 4x6", { prefix: "size" })` would _not_ use the prefix.