Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose camelCase and decamelize helpers #296

Merged
merged 1 commit into from Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion browser.js
Expand Up @@ -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 '' },
Expand All @@ -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
4 changes: 3 additions & 1 deletion deno.ts
Expand Up @@ -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'

Expand All @@ -27,9 +28,10 @@ const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial<Opt
const result = parser.parse(args.slice(), opts)
return result.argv
}

yargsParser.detailed = function (args: ArgsInput, opts?: Partial<Options>): DetailedArguments {
return parser.parse(args.slice(), opts)
}
yargsParser.camelCase = camelCase
yargsParser.decamelize = decamelize

export default yargsParser
3 changes: 3 additions & 0 deletions lib/index.ts
Expand Up @@ -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
Expand Down Expand Up @@ -46,4 +47,6 @@ const yargsParser: Parser = function Parser (args: ArgsInput, opts?: Partial<Opt
yargsParser.detailed = function (args: ArgsInput, opts?: Partial<Options>): DetailedArguments {
return parser.parse(args.slice(), opts)
}
yargsParser.camelCase = camelCase
yargsParser.decamelize = decamelize
export default yargsParser
2 changes: 2 additions & 0 deletions lib/yargs-parser-types.ts
Expand Up @@ -127,6 +127,8 @@ export type OptionsDefault = ValueOf<Pick<Required<Options>, 'default'>>;
export interface Parser {
(args: ArgsInput, opts?: Partial<Options>): Arguments;
detailed(args: ArgsInput, opts?: Partial<Options>): DetailedArguments;
camelCase(str: string): string;
decamelize(str: string, joinString?: string): string;
}

export type StringFlag = Dictionary<string[]>;
Expand Down
10 changes: 10 additions & 0 deletions test/deno/yargs-test.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
})
20 changes: 20 additions & 0 deletions 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')
})
})
})