match-casing
Match the case of value
to that of base
.
Install
This package is ESM only: Node 12+ is needed to use it and it must be import
ed
instead of require
d.
npm:
npm install match-casing
Use
import {matchCasing} from 'match-casing'
matchCasing('foo', 'BAZ')
matchCasing('foo', 'Baz')
matchCasing('foo', 'baz')
matchCasing('foo', 'BaZ')
matchCasing('FOO', 'BAZ')
matchCasing('FOO', 'Baz')
matchCasing('FOO', 'baz')
matchCasing('FOO', 'BaZ')
matchCasing('Foo', 'BAZ')
matchCasing('Foo', 'Baz')
matchCasing('Foo', 'baz')
matchCasing('Foo', 'BaZ')
matchCasing('’90S', '’twas')
matchCasing('’N’', 'a')
API
This package exports the following identifiers: matchCasing
.
There is no default export.
matchCasing(value, base)
Transform the case in value
(string
) to match that of base
(string
).
Algorithm
- If
base
is uppercase, value
is uppercased - Else, if
base
is lowercase, value
is lowercased - Else, if the first alphabetic character in
base
is uppercase,
and the rest of base
is lowercase, uppercase the first alphabetic
character in value
and lowercase the rest - Else, return
value
unmodified
The third case, where value
takes on a capitalized, deals with initial
non-alphabetical characters as expected.
License
MIT © Titus Wormer