Skip to content

Commit

Permalink
perf: implement simple camel-case, param-case and snake-case methods …
Browse files Browse the repository at this point in the history
…to improve performance
  • Loading branch information
c4spar committed Aug 22, 2020
1 parent 5c27a4b commit 20dc077
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 182 deletions.
25 changes: 14 additions & 11 deletions command/completions/zsh-completions-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import snakeCase from '../../x/snakeCase.ts';
import { Command } from '../command.ts';
import { IArgumentDetails, IOption } from '../types.ts';

Expand Down Expand Up @@ -28,15 +27,15 @@ export class ZshCompletionsGenerator {
const versionStr = version ? `# version: ${ this.cmd.getVersion() || '-' }\n#\n` : '';

return `
# compdef _${ snakeCase( this.cmd.getPath() ) } ${ this.cmd.getPath() }
# compdef _${ replaceSpecialChars( this.cmd.getPath() ) } ${ this.cmd.getPath() }
#
# zsh completion for ${ this.cmd.getPath() }
#
${versionStr}
autoload -U is-at-least
(( $+functions[__${ snakeCase( this.cmd.getName() ) }_complete] )) ||
function __${ snakeCase( this.cmd.getName() ) }_complete {
(( $+functions[__${ replaceSpecialChars( this.cmd.getName() ) }_complete] )) ||
function __${ replaceSpecialChars( this.cmd.getName() ) }_complete {
local name="$1"; shift
local action="$1"; shift
integer ret=1
Expand All @@ -57,9 +56,9 @@ function __${ snakeCase( this.cmd.getName() ) }_complete {
${ this.generateCompletions( this.cmd ).trim() }
# _${ snakeCase( this.cmd.getPath() ) } "\${@}"
# _${ replaceSpecialChars( this.cmd.getPath() ) } "\${@}"
compdef _${ snakeCase( this.cmd.getPath() ) } ${ this.cmd.getPath() }
compdef _${ replaceSpecialChars( this.cmd.getPath() ) } ${ this.cmd.getPath() }
#
# Local Variables:
Expand All @@ -83,8 +82,8 @@ compdef _${ snakeCase( this.cmd.getPath() ) } ${ this.cmd.getPath() }

path = ( path ? path + ' ' : '' ) + command.getName();

return `(( $+functions[_${ snakeCase( path ) }] )) ||
function _${ snakeCase( path ) }() {`
return `(( $+functions[_${ replaceSpecialChars( path ) }] )) ||
function _${ replaceSpecialChars( path ) }() {`
+ ( !command.getParent() ? `\n\n local context state state_descr line\n typeset -A opt_args` : '' )
+ this.generateCommandCompletions( command, path )
+ this.generateSubCommandCompletions( command, path )
Expand Down Expand Up @@ -124,7 +123,7 @@ function _${ snakeCase( path ) }() {`
const action = this.addAction( arg, completionsPath );

if ( action ) {
completions += `\n __${ snakeCase( this.cmd.getName() ) }_complete ${ action.arg.name } ${ action.arg.action } ${ action.cmd }`;
completions += `\n __${ replaceSpecialChars( this.cmd.getName() ) }_complete ${ action.arg.name } ${ action.arg.action } ${ action.cmd }`;
}
}

Expand All @@ -141,7 +140,7 @@ function _${ snakeCase( path ) }() {`

const actions: string = command
.getCommands( false )
.map( ( command: Command ) => `${ command.getName() }) _${ snakeCase( path + ' ' + command.getName() ) } ;;` )
.map( ( command: Command ) => `${ command.getName() }) _${ replaceSpecialChars( path + ' ' + command.getName() ) } ;;` )
.join( '\n ' );

return `\n
Expand Down Expand Up @@ -272,7 +271,7 @@ function _${ snakeCase( path ) }() {`
actions = Array
.from( this.actions )
.map( ( [ name, action ] ) =>
`${ name }) __${ snakeCase( this.cmd.getName() ) }_complete ${ action.arg.name } ${ action.arg.action } ${ action.cmd } ;;` );
`${ name }) __${ replaceSpecialChars( this.cmd.getName() ) }_complete ${ action.arg.name } ${ action.arg.action } ${ action.cmd } ;;` );
}

if ( command.hasCommands( false ) ) {
Expand All @@ -286,3 +285,7 @@ function _${ snakeCase( path ) }() {`
return '';
}
}

function replaceSpecialChars( str: string ): string {
return str.replace( /[^a-zA-Z0-9]/g, '_' );
}
2 changes: 1 addition & 1 deletion examples/prompt/checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env -S deno run --unstable

import { Checkbox } from '../../prompt/mod.ts';
import { Checkbox } from '../../prompt/checkbox.ts';

const colors: string[] = await Checkbox.prompt( {
message: `Pick a color`,
Expand Down
2 changes: 1 addition & 1 deletion examples/prompt/confirm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env -S deno run --unstable

import { Confirm } from '../../prompt/mod.ts';
import { Confirm } from '../../prompt/confirm.ts';

const confirmed: boolean = await Confirm.prompt( `Can you confirm?` );

Expand Down
7 changes: 7 additions & 0 deletions flags/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function camelCaseToParamCase( str: string ): string {
return str.replace( /([a-z])([A-Z])/g, '$1-$2' ).toLowerCase();
}

export function paramCaseToCamelCase( str: string ): string {
return str.replace( /-([a-z])/g, g => g[ 1 ].toUpperCase() );
}
4 changes: 2 additions & 2 deletions flags/flags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import camelCase from '../x/camelCase.ts';
import { paramCaseToCamelCase } from './_utils.ts';
import { normalize } from './normalize.ts';
import { IFlagArgument, IFlagOptions, IFlags, IFlagsResult, IFlagValue, IFlagValueType, IParseOptions, IType, OptionType } from './types.ts';
import { boolean } from './types/boolean.ts';
Expand Down Expand Up @@ -94,7 +94,7 @@ export function parseFlags<O = any>( args: string[], opts: IParseOptions = {} ):
throw new Error( `Missing name for option: ${ current }` );
}

const friendlyName: string = camelCase( option.name );
const friendlyName: string = paramCaseToCamelCase( option.name );

if ( typeof flags[ friendlyName ] !== 'undefined' && !option.collect ) {
throw new Error( `Duplicate option: ${ current }` );
Expand Down
12 changes: 6 additions & 6 deletions flags/validate-flags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import camelCase from '../x/camelCase.ts';
import paramCase from '../x/paramCase.ts';
import { camelCaseToParamCase, paramCaseToCamelCase } from './_utils.ts';
import { getOption } from './flags.ts';
import { IFlagArgument, IFlagOptions, IFlags, IFlagValue } from './types.ts';

Expand All @@ -23,7 +22,8 @@ export function validateFlags( flags: IFlagOptions[], values: IFlags, knownFlaks
const defaultValues: Record<string, boolean> = {};
// Set default value's
for ( const option of flags ) {
const name: string = camelCase( option.name );
const name: string = paramCaseToCamelCase( option.name );
// console.log('option.name:', option.name, name);
if ( typeof values[ name ] === 'undefined' && typeof option.default !== 'undefined' ) {
values[ name ] = typeof option.default === 'function' ? option.default() : option.default;
defaultValues[ name ] = true;
Expand All @@ -36,7 +36,7 @@ export function validateFlags( flags: IFlagOptions[], values: IFlags, knownFlaks
return;
}

const options: IFlagOptionsMap[] = keys.map( name => ( { name, option: getOption( flags, paramCase( name ) ) } ) );
const options: IFlagOptionsMap[] = keys.map( name => ( { name, option: getOption( flags, camelCaseToParamCase( name ) ) } ) );

for ( const { name, option } of options ) {

Expand Down Expand Up @@ -87,15 +87,15 @@ export function validateFlags( flags: IFlagOptions[], values: IFlags, knownFlaks
} );

function isset( flag: string ): boolean {
const name = camelCase( flag );
const name = paramCaseToCamelCase( flag );
// return typeof values[ name ] !== 'undefined' && values[ name ] !== false;
return typeof values[ name ] !== 'undefined';
}
}

for ( const option of flags ) {

if ( option.required && !( camelCase( option.name ) in values ) ) {
if ( option.required && !( paramCaseToCamelCase( option.name ) in values ) ) {

if ( (
!option.conflicts ||
Expand Down
20 changes: 0 additions & 20 deletions x/camelCase.ts

This file was deleted.

Empty file removed x/deps.ts
Empty file.
57 changes: 0 additions & 57 deletions x/lowerCase.ts

This file was deleted.

33 changes: 0 additions & 33 deletions x/normalCase.ts

This file was deleted.

5 changes: 0 additions & 5 deletions x/paramCase.ts

This file was deleted.

5 changes: 0 additions & 5 deletions x/snakeCase.ts

This file was deleted.

41 changes: 0 additions & 41 deletions x/upperCase.ts

This file was deleted.

0 comments on commit 20dc077

Please sign in to comment.