From 39154ceb5bdcf76b5f59a9219b34cedb79b67f26 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Wed, 5 Aug 2020 21:13:02 -0700 Subject: [PATCH] feat: expose camelCase and decamelize helpers (#296) --- browser.js | 4 +++- deno.ts | 4 +++- lib/index.ts | 3 +++ lib/yargs-parser-types.ts | 2 ++ test/deno/yargs-test.ts | 10 ++++++++++ test/string-utils.cjs | 20 ++++++++++++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/string-utils.cjs diff --git a/browser.js b/browser.js index 934edee1..cc5d2e00 100644 --- a/browser.js +++ b/browser.js @@ -2,6 +2,7 @@ // specific libraries, such as "path". // // TODO: figure out reasonable web equivalents for "resolve", "normalize", etc. +import { camelCase, decamelize } from './build/lib/string-utils.js' import { YargsParser } from './build/lib/yargs-parser.js' const parser = new YargsParser({ cwd: () => { return '' }, @@ -18,9 +19,10 @@ const yargsParser = function Parser (args, opts) { const result = parser.parse(args.slice(), opts) return result.argv } - yargsParser.detailed = function (args, opts) { return parser.parse(args.slice(), opts) } +yargsParser.camelCase = camelCase +yargsParser.decamelize = decamelize export default yargsParser diff --git a/deno.ts b/deno.ts index f60b7f7c..36bae99f 100644 --- a/deno.ts +++ b/deno.ts @@ -3,6 +3,7 @@ // // TODO: find reasonable replacement for require logic. import * as path from 'https://deno.land/std/path/mod.ts' +import { camelCase, decamelize } from './build/lib/string-utils.js' import { YargsParser } from './build/lib/yargs-parser.js' import { Arguments, ArgsInput, Parser, Options, DetailedArguments } from './build/lib/yargs-parser-types.d.ts' @@ -27,9 +28,10 @@ const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial): DetailedArguments { return parser.parse(args.slice(), opts) } +yargsParser.camelCase = camelCase +yargsParser.decamelize = decamelize export default yargsParser diff --git a/lib/index.ts b/lib/index.ts index d903020d..104e082d 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -4,6 +4,7 @@ import { format } from 'util' import { readFileSync } from 'fs' import { normalize, resolve } from 'path' import { ArgsInput, Arguments, Parser, Options, DetailedArguments } from './yargs-parser-types.js' +import { camelCase, decamelize } from './string-utils.js' import { YargsParser } from './yargs-parser.js' // 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): DetailedArguments { return parser.parse(args.slice(), opts) } +yargsParser.camelCase = camelCase +yargsParser.decamelize = decamelize export default yargsParser diff --git a/lib/yargs-parser-types.ts b/lib/yargs-parser-types.ts index 35136277..a1e2c4cf 100644 --- a/lib/yargs-parser-types.ts +++ b/lib/yargs-parser-types.ts @@ -127,6 +127,8 @@ export type OptionsDefault = ValueOf, 'default'>>; export interface Parser { (args: ArgsInput, opts?: Partial): Arguments; detailed(args: ArgsInput, opts?: Partial): DetailedArguments; + camelCase(str: string): string; + decamelize(str: string, joinString?: string): string; } export type StringFlag = Dictionary; diff --git a/test/deno/yargs-test.ts b/test/deno/yargs-test.ts index b766aa52..9a5442e7 100644 --- a/test/deno/yargs-test.ts +++ b/test/deno/yargs-test.ts @@ -5,6 +5,7 @@ import { } from 'https://deno.land/std/testing/asserts.ts' import parser from '../../deno.ts' +// Parser: Deno.test('parse string', () => { const parsed = parser('--foo --bar 99') assertEquals(parsed.foo, true) @@ -44,3 +45,12 @@ Deno.test('should load options and values from default config if specified', () assertEquals(argv.zoom, 55) assertEquals(argv.zoom, 55) }) + +// String utilities: +Deno.test('convert hyphenated string to camelcase', () => { + assertEquals(parser.camelCase('hello-world'), 'helloWorld') +}) + +Deno.test('convert camelcase string to hyphenated', () => { + assertEquals(parser.decamelize('helloWorld'), 'hello-world') +}) diff --git a/test/string-utils.cjs b/test/string-utils.cjs new file mode 100644 index 00000000..545311c2 --- /dev/null +++ b/test/string-utils.cjs @@ -0,0 +1,20 @@ +/* global describe, it */ + +const { expect } = require('chai') +const { camelCase, decamelize } = require('../build/index.cjs') + +describe('string-utils', function () { + describe('camelCase', () => { + it('converts string with hypen in middle to camel case', () => { + expect(camelCase('hello-world')).to.equal('helloWorld') + }) + it('removes leading hyphens', () => { + expect(camelCase('-goodnight-moon')).to.equal('goodnightMoon') + }) + }) + describe('decamelize', () => { + it('adds hyphens back to camelcase string', () => { + expect(decamelize('helloWorld')).to.equal('hello-world') + }) + }) +})