Skip to content

Commit 39154ce

Browse files
authored
feat: expose camelCase and decamelize helpers (#296)
1 parent fd91dde commit 39154ce

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

browser.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// specific libraries, such as "path".
33
//
44
// TODO: figure out reasonable web equivalents for "resolve", "normalize", etc.
5+
import { camelCase, decamelize } from './build/lib/string-utils.js'
56
import { YargsParser } from './build/lib/yargs-parser.js'
67
const parser = new YargsParser({
78
cwd: () => { return '' },
@@ -18,9 +19,10 @@ const yargsParser = function Parser (args, opts) {
1819
const result = parser.parse(args.slice(), opts)
1920
return result.argv
2021
}
21-
2222
yargsParser.detailed = function (args, opts) {
2323
return parser.parse(args.slice(), opts)
2424
}
25+
yargsParser.camelCase = camelCase
26+
yargsParser.decamelize = decamelize
2527

2628
export default yargsParser

deno.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
// TODO: find reasonable replacement for require logic.
55
import * as path from 'https://deno.land/std/path/mod.ts'
6+
import { camelCase, decamelize } from './build/lib/string-utils.js'
67
import { YargsParser } from './build/lib/yargs-parser.js'
78
import { Arguments, ArgsInput, Parser, Options, DetailedArguments } from './build/lib/yargs-parser-types.d.ts'
89

@@ -27,9 +28,10 @@ const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial<Opt
2728
const result = parser.parse(args.slice(), opts)
2829
return result.argv
2930
}
30-
3131
yargsParser.detailed = function (args: ArgsInput, opts?: Partial<Options>): DetailedArguments {
3232
return parser.parse(args.slice(), opts)
3333
}
34+
yargsParser.camelCase = camelCase
35+
yargsParser.decamelize = decamelize
3436

3537
export default yargsParser

lib/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { format } from 'util'
44
import { readFileSync } from 'fs'
55
import { normalize, resolve } from 'path'
66
import { ArgsInput, Arguments, Parser, Options, DetailedArguments } from './yargs-parser-types.js'
7+
import { camelCase, decamelize } from './string-utils.js'
78
import { YargsParser } from './yargs-parser.js'
89

910
// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
@@ -46,4 +47,6 @@ const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial<Opt
4647
yargsParser.detailed = function (args: ArgsInput, opts?: Partial<Options>): DetailedArguments {
4748
return parser.parse(args.slice(), opts)
4849
}
50+
yargsParser.camelCase = camelCase
51+
yargsParser.decamelize = decamelize
4952
export default yargsParser

lib/yargs-parser-types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ export type OptionsDefault = ValueOf<Pick<Required<Options>, 'default'>>;
127127
export interface Parser {
128128
(args: ArgsInput, opts?: Partial<Options>): Arguments;
129129
detailed(args: ArgsInput, opts?: Partial<Options>): DetailedArguments;
130+
camelCase(str: string): string;
131+
decamelize(str: string, joinString?: string): string;
130132
}
131133

132134
export type StringFlag = Dictionary<string[]>;

test/deno/yargs-test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from 'https://deno.land/std/testing/asserts.ts'
66
import parser from '../../deno.ts'
77

8+
// Parser:
89
Deno.test('parse string', () => {
910
const parsed = parser('--foo --bar 99')
1011
assertEquals(parsed.foo, true)
@@ -44,3 +45,12 @@ Deno.test('should load options and values from default config if specified', ()
4445
assertEquals(argv.zoom, 55)
4546
assertEquals(argv.zoom, 55)
4647
})
48+
49+
// String utilities:
50+
Deno.test('convert hyphenated string to camelcase', () => {
51+
assertEquals(parser.camelCase('hello-world'), 'helloWorld')
52+
})
53+
54+
Deno.test('convert camelcase string to hyphenated', () => {
55+
assertEquals(parser.decamelize('helloWorld'), 'hello-world')
56+
})

test/string-utils.cjs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* global describe, it */
2+
3+
const { expect } = require('chai')
4+
const { camelCase, decamelize } = require('../build/index.cjs')
5+
6+
describe('string-utils', function () {
7+
describe('camelCase', () => {
8+
it('converts string with hypen in middle to camel case', () => {
9+
expect(camelCase('hello-world')).to.equal('helloWorld')
10+
})
11+
it('removes leading hyphens', () => {
12+
expect(camelCase('-goodnight-moon')).to.equal('goodnightMoon')
13+
})
14+
})
15+
describe('decamelize', () => {
16+
it('adds hyphens back to camelcase string', () => {
17+
expect(decamelize('helloWorld')).to.equal('hello-world')
18+
})
19+
})
20+
})

0 commit comments

Comments
 (0)