Skip to content

Commit 435f1df

Browse files
committed
Add case functions
Refs: metarhia/impress#1710 PR-URL: #98
1 parent 3994dab commit 435f1df

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased][unreleased]
44

55
- Add function `nowDateTimeUTC(date?: Date, timeSep?: string): string`
6+
- Add case functions: `toLower`, `toCamel`, `spinalToCamel`, and `snakeToCamel`
67

78
## [3.5.16][] - 2021-10-10
89

lib/utilities.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ const toLowerCamel = (s) => s.charAt(0).toLowerCase() + s.slice(1);
8585

8686
const toUpperCamel = (s) => s.charAt(0).toUpperCase() + s.slice(1);
8787

88+
const toLower = (s) => s.toLowerCase();
89+
90+
const toCamel = (separator) => (s) => {
91+
const words = s.split(separator);
92+
const first = words.length > 0 ? words.shift().toLowerCase() : '';
93+
return first + words.map(toLower).map(toUpperCamel).join('');
94+
};
95+
96+
const spinalToCamel = toCamel('-');
97+
98+
const snakeToCamel = toCamel('_');
99+
88100
const isConstant = (s) => s === s.toUpperCase();
89101

90102
const twoDigit = (n) => {
@@ -325,6 +337,10 @@ module.exports = {
325337
isFirstUpper,
326338
toLowerCamel,
327339
toUpperCamel,
340+
toLower,
341+
toCamel,
342+
spinalToCamel,
343+
snakeToCamel,
328344
isConstant,
329345
nowDate,
330346
nowDateTimeUTC,

metautil.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export function between(s: string, prefix: string, suffix: string): string;
3333
export function isFirstUpper(s: string): boolean;
3434
export function toLowerCamel(s: string): string;
3535
export function toUpperCamel(s: string): string;
36+
export function toLower(s: string): string;
37+
export function toCamel(separator: string): (s: string) => string;
38+
export function spinalToCamel(s: string): string;
39+
export function snakeToCamel(s: string): string;
3640
export function isConstant(s: string): boolean;
3741
export function nowDate(date?: Date): string;
3842
export function nowDateTimeUTC(date?: Date, timeSep?: string): string;

test/utilities.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,34 @@ metatests.case(
9292
['aBcD', 'ABcD'],
9393
['', ''],
9494
],
95+
'metautil.toLower': [
96+
['abcd', 'abcd'],
97+
['ABCD', 'abcd'],
98+
['', ''],
99+
],
100+
'metautil.toCamel': [
101+
['-', (f) => f('ab-cd') === 'abCd'],
102+
['--', (f) => f('AB--CD') === 'abCd'],
103+
['', (f) => f('') === ''],
104+
],
105+
'metautil.spinalToCamel': [
106+
['abcd', 'abcd'],
107+
['ab-cd', 'abCd'],
108+
['AB-CD', 'abCd'],
109+
['AB-CD', 'abCd'],
110+
['a', 'a'],
111+
['', ''],
112+
['-', ''],
113+
],
114+
'metautil.snakeToCamel': [
115+
['abcd', 'abcd'],
116+
['ab_cd', 'abCd'],
117+
['AB_CD', 'abCd'],
118+
['AB_CD', 'abCd'],
119+
['a', 'a'],
120+
['', ''],
121+
['_', ''],
122+
],
95123
'metautil.isConstant': [
96124
['UPPER', true],
97125
['UPPER_SNAKE', true],

0 commit comments

Comments
 (0)