change-case
Advanced tools
Comparing version 5.3.0 to 5.4.0
@@ -15,4 +15,7 @@ /** | ||
*/ | ||
export interface Options extends SplitOptions { | ||
export interface Options { | ||
locale?: Locale; | ||
split?: (value: string) => string[]; | ||
/** @deprecated Pass `split: splitSeparateNumbers` instead. */ | ||
separateNumbers?: boolean; | ||
delimiter?: string; | ||
@@ -23,11 +26,9 @@ prefixCharacters?: string; | ||
/** | ||
* Options used for splitting strings into word segments. | ||
* Split any cased input strings into an array of words. | ||
*/ | ||
export interface SplitOptions { | ||
separateNumbers?: boolean; | ||
} | ||
export declare function split(value: string): string[]; | ||
/** | ||
* Split any cased input strings into an array of words. | ||
* Split the input string into an array of words, separating numbers. | ||
*/ | ||
export declare function split(value: string, options?: SplitOptions): string[]; | ||
export declare function splitSeparateNumbers(value: string): string[]; | ||
/** | ||
@@ -34,0 +35,0 @@ * Convert a string to space separated lower case (`foo bar`). |
// 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; | ||
// Used to iterate over the initial split result and separate numbers. | ||
const SPLIT_SEPARATE_NUMBER_RE = /(?<=\d)(\p{Ll})|(?<=\p{L})(\d)/u; | ||
// Regexp involved with stripping non-word characters from the result. | ||
@@ -15,3 +15,3 @@ const DEFAULT_STRIP_REGEXP = /[^\p{L}\d]+/giu; | ||
*/ | ||
export function split(value, options) { | ||
export function split(value) { | ||
let result = value.trim(); | ||
@@ -21,7 +21,2 @@ result = result | ||
.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"); | ||
@@ -40,10 +35,22 @@ let start = 0; | ||
/** | ||
* Split the input string into an array of words, separating numbers. | ||
*/ | ||
export function splitSeparateNumbers(value) { | ||
const words = split(value); | ||
for (let i = 0; i < words.length; i++) { | ||
const word = words[i]; | ||
const match = SPLIT_SEPARATE_NUMBER_RE.exec(word); | ||
if (match) { | ||
words.splice(i, 1, word.slice(0, match.index), word.slice(match.index)); | ||
} | ||
} | ||
return words; | ||
} | ||
/** | ||
* Convert a string to space separated lower case (`foo bar`). | ||
*/ | ||
export function noCase(input, options) { | ||
const [prefix, value, suffix] = splitPrefixSuffix(input, options); | ||
const [prefix, words, suffix] = splitPrefixSuffix(input, options); | ||
return (prefix + | ||
split(value, options) | ||
.map(lowerFactory(options?.locale)) | ||
.join(options?.delimiter ?? " ") + | ||
words.map(lowerFactory(options?.locale)).join(options?.delimiter ?? " ") + | ||
suffix); | ||
@@ -55,3 +62,3 @@ } | ||
export function camelCase(input, options) { | ||
const [prefix, value, suffix] = splitPrefixSuffix(input, options); | ||
const [prefix, words, suffix] = splitPrefixSuffix(input, options); | ||
const lower = lowerFactory(options?.locale); | ||
@@ -63,3 +70,3 @@ const upper = upperFactory(options?.locale); | ||
return (prefix + | ||
split(value, options) | ||
words | ||
.map((word, index) => { | ||
@@ -77,3 +84,3 @@ if (index === 0) | ||
export function pascalCase(input, options) { | ||
const [prefix, value, suffix] = splitPrefixSuffix(input, options); | ||
const [prefix, words, suffix] = splitPrefixSuffix(input, options); | ||
const lower = lowerFactory(options?.locale); | ||
@@ -84,7 +91,3 @@ const upper = upperFactory(options?.locale); | ||
: pascalCaseTransformFactory(lower, upper); | ||
return (prefix + | ||
split(value, options) | ||
.map(transform) | ||
.join(options?.delimiter ?? "") + | ||
suffix); | ||
return prefix + words.map(transform).join(options?.delimiter ?? "") + suffix; | ||
} | ||
@@ -101,7 +104,7 @@ /** | ||
export function capitalCase(input, options) { | ||
const [prefix, value, suffix] = splitPrefixSuffix(input, options); | ||
const [prefix, words, suffix] = splitPrefixSuffix(input, options); | ||
const lower = lowerFactory(options?.locale); | ||
const upper = upperFactory(options?.locale); | ||
return (prefix + | ||
split(value, options) | ||
words | ||
.map(capitalCaseTransformFactory(lower, upper)) | ||
@@ -115,7 +118,5 @@ .join(options?.delimiter ?? " ") + | ||
export function constantCase(input, options) { | ||
const [prefix, value, suffix] = splitPrefixSuffix(input, options); | ||
const [prefix, words, suffix] = splitPrefixSuffix(input, options); | ||
return (prefix + | ||
split(value, options) | ||
.map(upperFactory(options?.locale)) | ||
.join(options?.delimiter ?? "_") + | ||
words.map(upperFactory(options?.locale)).join(options?.delimiter ?? "_") + | ||
suffix); | ||
@@ -145,3 +146,3 @@ } | ||
export function sentenceCase(input, options) { | ||
const [prefix, value, suffix] = splitPrefixSuffix(input, options); | ||
const [prefix, words, suffix] = splitPrefixSuffix(input, options); | ||
const lower = lowerFactory(options?.locale); | ||
@@ -151,3 +152,3 @@ const upper = upperFactory(options?.locale); | ||
return (prefix + | ||
split(value, options) | ||
words | ||
.map((word, index) => { | ||
@@ -193,5 +194,6 @@ if (index === 0) | ||
} | ||
function splitPrefixSuffix(input, options) { | ||
const prefixCharacters = options?.prefixCharacters ?? DEFAULT_PREFIX_SUFFIX_CHARACTERS; | ||
const suffixCharacters = options?.suffixCharacters ?? DEFAULT_PREFIX_SUFFIX_CHARACTERS; | ||
function splitPrefixSuffix(input, options = {}) { | ||
const splitFn = options.split ?? (options.separateNumbers ? splitSeparateNumbers : split); | ||
const prefixCharacters = options.prefixCharacters ?? DEFAULT_PREFIX_SUFFIX_CHARACTERS; | ||
const suffixCharacters = options.suffixCharacters ?? DEFAULT_PREFIX_SUFFIX_CHARACTERS; | ||
let prefixIndex = 0; | ||
@@ -214,3 +216,3 @@ let suffixIndex = input.length; | ||
input.slice(0, prefixIndex), | ||
input.slice(prefixIndex, suffixIndex), | ||
splitFn(input.slice(prefixIndex, suffixIndex)), | ||
input.slice(suffixIndex), | ||
@@ -217,0 +219,0 @@ ]; |
{ | ||
"name": "change-case", | ||
"version": "5.3.0", | ||
"version": "5.4.0", | ||
"description": "Transform a string between `camelCase`, `PascalCase`, `Capital Case`, `snake_case`, `kebab-case`, `CONSTANT_CASE` and others", | ||
@@ -32,10 +32,4 @@ "keywords": [ | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js" | ||
}, | ||
"./keys": { | ||
"types": "./dist/keys.d.ts", | ||
"import": "./dist/keys.js" | ||
} | ||
".": "./dist/index.js", | ||
"./keys": "./dist/keys.js" | ||
}, | ||
@@ -42,0 +36,0 @@ "types": "./dist/index.d.ts", |
@@ -40,3 +40,3 @@ # Change Case | ||
- `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 `false`. | ||
- `split?: (value: string) => string[]` A function to define how the input is split into words. Defaults to `split`. | ||
- `prefixCharacters?: string` Retain at the beginning of the string. Defaults to `""`. Example: use `"_"` to keep the underscores in `__typename`. | ||
@@ -43,0 +43,0 @@ - `suffixCharacters?: string` Retain at the end of the string. Defaults to `""`. Example: use `"_"` to keep the underscore in `type_`. |
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
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
34968
327