change-case
Advanced tools
Comparing version 4.1.2 to 5.0.0
@@ -1,11 +0,63 @@ | ||
export * from "camel-case"; | ||
export * from "capital-case"; | ||
export * from "constant-case"; | ||
export * from "dot-case"; | ||
export * from "header-case"; | ||
export * from "no-case"; | ||
export * from "param-case"; | ||
export * from "pascal-case"; | ||
export * from "path-case"; | ||
export * from "sentence-case"; | ||
export * from "snake-case"; | ||
/** | ||
* Supported locale values. Use `false` to ignore locale. | ||
* Defaults to `undefined`, which uses the host environment. | ||
*/ | ||
export type Locale = string[] | string | false | undefined; | ||
export interface Options extends SplitOptions { | ||
locale?: Locale; | ||
} | ||
/** | ||
* Convert a string to space separated lower case (`foo bar`). | ||
*/ | ||
export declare function noCase(input: string, options?: Options): string; | ||
export interface SplitOptions { | ||
separateNumbers?: boolean; | ||
} | ||
/** | ||
* Split any cased input strings into an array of words. | ||
*/ | ||
export declare function split(input: string, options?: SplitOptions): string[]; | ||
/** | ||
* Convert a string to camel case (`fooBar`). | ||
*/ | ||
export declare function camelCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to pascal case (`FooBar`). | ||
*/ | ||
export declare function pascalCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to pascal snake case (`Foo_Bar`). | ||
*/ | ||
export declare function pascalSnakeCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to capital case (`Foo Bar`). | ||
*/ | ||
export declare function capitalCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to constant case (`FOO_BAR`). | ||
*/ | ||
export declare function constantCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to dot case (`foo.bar`). | ||
*/ | ||
export declare function dotCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to kebab case (`foo-bar`). | ||
*/ | ||
export declare function kebabCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to path case (`foo/bar`). | ||
*/ | ||
export declare function pathCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to path case (`Foo bar`). | ||
*/ | ||
export declare function sentenceCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to snake case (`foo_bar`). | ||
*/ | ||
export declare function snakeCase(input: string, options?: Options): string; | ||
/** | ||
* Convert a string to header case (`Foo-Bar`). | ||
*/ | ||
export declare function trainCase(input: string, options?: Options): string; |
@@ -1,15 +0,168 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("camel-case"), exports); | ||
tslib_1.__exportStar(require("capital-case"), exports); | ||
tslib_1.__exportStar(require("constant-case"), exports); | ||
tslib_1.__exportStar(require("dot-case"), exports); | ||
tslib_1.__exportStar(require("header-case"), exports); | ||
tslib_1.__exportStar(require("no-case"), exports); | ||
tslib_1.__exportStar(require("param-case"), exports); | ||
tslib_1.__exportStar(require("pascal-case"), exports); | ||
tslib_1.__exportStar(require("path-case"), exports); | ||
tslib_1.__exportStar(require("sentence-case"), exports); | ||
tslib_1.__exportStar(require("snake-case"), exports); | ||
/** | ||
* Convert a string to space separated lower case (`foo bar`). | ||
*/ | ||
export function noCase(input, options) { | ||
return split(input, options) | ||
.map(lowerFactory(options?.locale)) | ||
.join(" "); | ||
} | ||
// Regexps involved with splitting words in various case formats. | ||
const SPLIT_LOWER_UPPER_RE = /([\p{Ll}\d])(\p{Lu})/gu; | ||
const SPLIT_UPPER_UPPER_RE = /(\p{Lu})([\p{Lu}][\p{Ll}])/gu; | ||
const SPLIT_NUMBER_LOWER_RE = /(\d)(\p{Ll})/gu; | ||
const SPLIT_LETTER_NUMBER_RE = /(\p{L})(\d)/gu; | ||
// Regexp involved with stripping non-word characters from the result. | ||
const DEFAULT_STRIP_REGEXP = /[^\p{L}\d]+/giu; | ||
// The replacement value for splits. | ||
const SPLIT_REPLACE_VALUE = "$1\0$2"; | ||
/** | ||
* Split any cased input strings into an array of words. | ||
*/ | ||
export function split(input, options = {}) { | ||
let result = input | ||
.replace(SPLIT_LOWER_UPPER_RE, SPLIT_REPLACE_VALUE) | ||
.replace(SPLIT_UPPER_UPPER_RE, SPLIT_REPLACE_VALUE); | ||
if (options.separateNumbers) { | ||
result = result | ||
.replace(SPLIT_NUMBER_LOWER_RE, SPLIT_REPLACE_VALUE) | ||
.replace(SPLIT_LETTER_NUMBER_RE, SPLIT_REPLACE_VALUE); | ||
} | ||
result = result.replace(DEFAULT_STRIP_REGEXP, "\0"); | ||
let start = 0; | ||
let end = result.length; | ||
// Trim the delimiter from around the output string. | ||
while (result.charAt(start) === "\0") | ||
start++; | ||
if (start === end) | ||
return []; | ||
while (result.charAt(end - 1) === "\0") | ||
end--; | ||
// Transform each token independently. | ||
return result.slice(start, end).split(/\0/g); | ||
} | ||
/** | ||
* Convert a string to camel case (`fooBar`). | ||
*/ | ||
export function camelCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
const upper = upperFactory(options?.locale); | ||
const transform = pascalCaseTransformFactory(lower, upper); | ||
return split(input, options) | ||
.map((word, index) => { | ||
if (index === 0) | ||
return lower(word); | ||
return transform(word, index); | ||
}) | ||
.join(""); | ||
} | ||
/** | ||
* Convert a string to pascal case (`FooBar`). | ||
*/ | ||
export function pascalCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
const upper = upperFactory(options?.locale); | ||
return split(input, options) | ||
.map(pascalCaseTransformFactory(lower, upper)) | ||
.join(""); | ||
} | ||
/** | ||
* Convert a string to pascal snake case (`Foo_Bar`). | ||
*/ | ||
export function pascalSnakeCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
const upper = upperFactory(options?.locale); | ||
return split(input, options) | ||
.map(capitalCaseTransformFactory(lower, upper)) | ||
.join("_"); | ||
} | ||
/** | ||
* Convert a string to capital case (`Foo Bar`). | ||
*/ | ||
export function capitalCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
const upper = upperFactory(options?.locale); | ||
return split(input, options) | ||
.map(capitalCaseTransformFactory(lower, upper)) | ||
.join(" "); | ||
} | ||
/** | ||
* Convert a string to constant case (`FOO_BAR`). | ||
*/ | ||
export function constantCase(input, options) { | ||
const upper = upperFactory(options?.locale); | ||
return split(input, options).map(upper).join("_"); | ||
} | ||
/** | ||
* Convert a string to dot case (`foo.bar`). | ||
*/ | ||
export function dotCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
return split(input, options).map(lower).join("."); | ||
} | ||
/** | ||
* Convert a string to kebab case (`foo-bar`). | ||
*/ | ||
export function kebabCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
return split(input, options).map(lower).join("-"); | ||
} | ||
/** | ||
* Convert a string to path case (`foo/bar`). | ||
*/ | ||
export function pathCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
return split(input, options).map(lower).join("/"); | ||
} | ||
/** | ||
* Convert a string to path case (`Foo bar`). | ||
*/ | ||
export function sentenceCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
const upper = upperFactory(options?.locale); | ||
const transform = capitalCaseTransformFactory(lower, upper); | ||
return split(input, options) | ||
.map((word, index) => { | ||
if (index === 0) | ||
return transform(word); | ||
return lower(word); | ||
}) | ||
.join(" "); | ||
} | ||
/** | ||
* Convert a string to snake case (`foo_bar`). | ||
*/ | ||
export function snakeCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
return split(input, options).map(lower).join("_"); | ||
} | ||
/** | ||
* Convert a string to header case (`Foo-Bar`). | ||
*/ | ||
export function trainCase(input, options) { | ||
const lower = lowerFactory(options?.locale); | ||
const upper = upperFactory(options?.locale); | ||
return split(input, options) | ||
.map(capitalCaseTransformFactory(lower, upper)) | ||
.join("-"); | ||
} | ||
function lowerFactory(locale) { | ||
return locale === false | ||
? (input) => input.toLowerCase() | ||
: (input) => input.toLocaleLowerCase(locale); | ||
} | ||
function upperFactory(locale) { | ||
return locale === false | ||
? (input) => input.toUpperCase() | ||
: (input) => input.toLocaleUpperCase(locale); | ||
} | ||
function capitalCaseTransformFactory(lower, upper) { | ||
return (word) => `${upper(word[0])}${lower(word.slice(1))}`; | ||
} | ||
function pascalCaseTransformFactory(lower, upper) { | ||
return (word, index) => { | ||
const char0 = word[0]; | ||
const initial = index > 0 && char0 >= "0" && char0 <= "9" ? "_" + char0 : upper(char0); | ||
return initial + lower(word.slice(1)); | ||
}; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "change-case", | ||
"version": "4.1.2", | ||
"description": "Transform a string between `camelCase`, `PascalCase`, `Capital Case`, `snake_case`, `param-case`, `CONSTANT_CASE` and others", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"module": "dist.es2015/index.js", | ||
"sideEffects": false, | ||
"jsnext:main": "dist.es2015/index.js", | ||
"version": "5.0.0", | ||
"description": "Transform a string between `camelCase`, `PascalCase`, `Capital Case`, `snake_case`, `kebab-case`, `CONSTANT_CASE` and others", | ||
"type": "module", | ||
"files": [ | ||
"dist/", | ||
"dist.es2015/", | ||
"LICENSE" | ||
"dist/" | ||
], | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./keys": "./dist/keys.js" | ||
}, | ||
"scripts": { | ||
"lint": "tslint \"src/**/*\" --project tsconfig.json", | ||
"build": "rimraf dist/ dist.es2015/ && tsc && tsc -P tsconfig.es2015.json", | ||
"specs": "jest --coverage", | ||
"test": "npm run build && npm run lint && npm run specs", | ||
"size": "size-limit", | ||
"prepare": "npm run build" | ||
"format": "ts-scripts format", | ||
"specs": "ts-scripts specs", | ||
"test": "ts-scripts test", | ||
"build": "ts-scripts build", | ||
"bench": "vitest bench", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
@@ -47,56 +45,6 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/blakeembrey/change-case/tree/master/packages/camel-case#readme", | ||
"size-limit": [ | ||
{ | ||
"path": "dist/index.js", | ||
"limit": "750 B" | ||
} | ||
], | ||
"jest": { | ||
"roots": [ | ||
"<rootDir>/src/" | ||
], | ||
"transform": { | ||
"\\.tsx?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
] | ||
}, | ||
"homepage": "https://github.com/blakeembrey/change-case/tree/master/packages/change-case#readme", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"camel-case": "^4.1.2", | ||
"capital-case": "^1.0.4", | ||
"constant-case": "^3.0.4", | ||
"dot-case": "^3.0.4", | ||
"header-case": "^2.0.4", | ||
"no-case": "^3.0.4", | ||
"param-case": "^3.0.4", | ||
"pascal-case": "^3.1.2", | ||
"path-case": "^3.0.4", | ||
"sentence-case": "^3.0.4", | ||
"snake-case": "^3.0.4", | ||
"tslib": "^2.0.3" | ||
}, | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^2.2.1", | ||
"@types/jest": "^24.0.23", | ||
"@types/node": "^12.12.14", | ||
"jest": "^24.9.0", | ||
"rimraf": "^3.0.0", | ||
"ts-jest": "^24.2.0", | ||
"tslint": "^5.20.1", | ||
"tslint-config-prettier": "^1.18.0", | ||
"tslint-config-standard": "^9.0.0", | ||
"typescript": "^4.1.2" | ||
}, | ||
"gitHead": "76a21a7f6f2a226521ef6abd345ff309cbd01fb0" | ||
} | ||
} |
# Change Case | ||
[![NPM version][npm-image]][npm-url] | ||
[![NPM downloads][downloads-image]][downloads-url] | ||
[![Bundle size][bundlephobia-image]][bundlephobia-url] | ||
> Transform a string between `camelCase`, `PascalCase`, `Capital Case`, `snake_case`, `kebab-case`, `CONSTANT_CASE` and others. | ||
> Transform a string between `camelCase`, `PascalCase`, `Capital Case`, `snake_case`, `param-case`, `CONSTANT_CASE` and others. | ||
## Installation | ||
@@ -17,29 +13,53 @@ | ||
These case change functions are included: | ||
| Method | Result | | ||
| -------------- | ----------- | | ||
| `camelCase` | `twoWords` | | ||
| `capitalCase` | `Two Words` | | ||
| `constantCase` | `TWO_WORDS` | | ||
| `dotCase` | `two.words` | | ||
| `trainCase` | `Two-Words` | | ||
| `kebabCase` | `two-words` | | ||
| `noCase` | `two words` | | ||
| `pascalCase` | `TwoWords` | | ||
| `pathCase` | `two/words` | | ||
| `sentenceCase` | `Two words` | | ||
| `snakeCase` | `two_words` | | ||
All core methods accept [`options`](#options) as the second argument. | ||
### Options | ||
- `locale?: string[] | string | false` - lower/upper according to specified locale, defaults to host environment. Set to `false` to disable. | ||
- `separateNumbers?: boolean` Splits `foo123` into `foo 123` instead of keeping them together. Defaults to `true`. | ||
### Split | ||
**Change case** also exports a `split` function which can be used to build your own case formatting methods. It accepts a string and returns each "word" as an array. For example: | ||
```js | ||
import { | ||
camelCase, | ||
capitalCase, | ||
constantCase, | ||
dotCase, | ||
headerCase, | ||
noCase, | ||
paramCase, | ||
pascalCase, | ||
pathCase, | ||
sentenceCase, | ||
snakeCase, | ||
} from "change-case"; | ||
split("fooBar") | ||
.map((x) => x.toLowerCase()) | ||
.join("_"); //=> "foo_bar" | ||
``` | ||
Methods can also be installed [independently](https://github.com/blakeembrey/change-case). All functions also accept [`options`](https://github.com/blakeembrey/change-case#options) as the second argument. | ||
## Change Case Keys | ||
```js | ||
import * as changeKeys from "change-case/keys"; | ||
changeKeys.camelCase({ TEST_KEY: true }); //=> { testKey: true } | ||
``` | ||
Keys is a wrapper around all case methods to support transforming objects to any case. | ||
### API | ||
- **input: unknown** Any JavaScript value. | ||
- **depth: number** Specify the depth to transfer for case transformation. Defaults to `1`. | ||
- **options: object** Same as base case library. | ||
## License | ||
MIT | ||
[npm-image]: https://img.shields.io/npm/v/change-case.svg?style=flat | ||
[npm-url]: https://npmjs.org/package/change-case | ||
[downloads-image]: https://img.shields.io/npm/dm/change-case.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/change-case | ||
[bundlephobia-image]: https://img.shields.io/bundlephobia/minzip/change-case.svg | ||
[bundlephobia-url]: https://bundlephobia.com/result?p=change-case |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
28098
0
0
271
0
65
Yes
8
1
- Removedcamel-case@^4.1.2
- Removedcapital-case@^1.0.4
- Removedconstant-case@^3.0.4
- Removeddot-case@^3.0.4
- Removedheader-case@^2.0.4
- Removedno-case@^3.0.4
- Removedparam-case@^3.0.4
- Removedpascal-case@^3.1.2
- Removedpath-case@^3.0.4
- Removedsentence-case@^3.0.4
- Removedsnake-case@^3.0.4
- Removedtslib@^2.0.3
- Removedcamel-case@4.1.2(transitive)
- Removedcapital-case@1.0.4(transitive)
- Removedconstant-case@3.0.4(transitive)
- Removeddot-case@3.0.4(transitive)
- Removedheader-case@2.0.4(transitive)
- Removedlower-case@2.0.2(transitive)
- Removedno-case@3.0.4(transitive)
- Removedparam-case@3.0.4(transitive)
- Removedpascal-case@3.1.2(transitive)
- Removedpath-case@3.0.4(transitive)
- Removedsentence-case@3.0.4(transitive)
- Removedsnake-case@3.0.4(transitive)
- Removedtslib@2.8.1(transitive)
- Removedupper-case@2.0.2(transitive)
- Removedupper-case-first@2.0.2(transitive)