Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alextanhongpin/stringcase
Stringcase (lettercase) detection and conversion for JavaScript
https://github.com/alextanhongpin/stringcase
js lettercase stringcase
Last synced: about 1 month ago
JSON representation
Stringcase (lettercase) detection and conversion for JavaScript
- Host: GitHub
- URL: https://github.com/alextanhongpin/stringcase
- Owner: alextanhongpin
- Created: 2020-08-04T17:42:03.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T13:18:23.000Z (about 2 years ago)
- Last Synced: 2024-11-03T02:36:41.241Z (2 months ago)
- Topics: js, lettercase, stringcase
- Language: JavaScript
- Homepage: https://alextanhongpin.github.io/stringcase/
- Size: 910 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @alextanhongpin/stringcase
A package to auto-detect lettercase, and converts to different format.
## Installation
```bash
$ npm i @alextanhongpin/stringcase
```## Usage
```js
const assert = require("assert");
const {
stringCase,
StringCase,
CamelCase
} = require("@alextanhongpin/stringcase");// Factory method, produces a class instance that extends StringCase.
// Can be either CamelCase, SnakeCase, PascalCase, KebabCase,
// UpperCase, LowerCase or UnknownCase.
const greeting = stringCase("helloWorld");assert(greeting instanceof StringCase);
assert(greeting instanceof CamelCase);
assert.equal("helloWorld", greeting.camel);
assert.equal("HelloWorld", greeting.pascal);
assert.equal("hello-world", greeting.kebab);
assert.equal("hello_world", greeting.snake);
```## Use Cases
- detecting the difference type of cases allows you to predict what language is being used, e.g. snake case is most likely SQL or Python
- converting between different cases, useful for code generation, where a user just provide a file name,e
E.g. user-service, and UserService can be generated
- counting occurances of a particular case to detect trends, if a lot of text has underscore, the text is most likely related to tech article, if camel case then JS code snippets
- finding wrong case, e.g. if the code snippet case should only be snake case, but a camel case is detected
- normalizing cases, say we are extracting headers from CSV and we want to dynamically generate field names (or compare to existing ones for validation), we can convert all to camel case
- slugifying string, creating enums out of strings dynamically. There are better libraries for this, but if all you need is to generate enums from dynamic fields, e.g. When user provide an input string for status `order delivered`, we can convert it to snake case and upper case it, so that it becomes `ORDER_DELIVERED` before storing it in the database. Note: you can write a postgres user defined function to achieve the same. Note that this approach assumes that the language used is only English, and there are no localisation involved.