diff --git a/src/utils.ts b/src/utils.ts index 2d5f78e..9a8fd7b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,14 @@ import { isArray } from 'rattail'; import type { Alias } from './types'; +/** + * Check all values in array are same to target + * @returns {boolean} + */ +export function allNotEqualToTarget(values: T[], target: T) { + return values.every((value) => value !== target); +} + /** * Check alias name or command is valid * @@ -18,10 +26,9 @@ import type { Alias } from './types'; * ``` * * @param {value} string - * @param {boolean} [skipNoQuote=false] - whether to skip no quote validation * @returns If it is valid, return true */ -function isValid(value: string, skipNoQuote = false) { +export function isValid(value: string) { const firstChar = value.charAt(0); const lastChar = value.charAt(value.length - 1); @@ -33,7 +40,7 @@ function isValid(value: string, skipNoQuote = false) { return true; } - if (![`'`, `"`].includes(firstChar) && ![`'`, `"`].includes(lastChar) && !skipNoQuote) { + if (![`'`, `"`].includes(firstChar) && ![`'`, `"`].includes(lastChar)) { return true; } @@ -77,6 +84,16 @@ function isValid(value: string, skipNoQuote = false) { * resolveAlias(`alias "nv"="node -v"`) // { aliasName: 'nv', command: 'node -v' } * ``` * + * @example + * ```typescript + * resolveAlias(`alias nv=node -v`) // { aliasName: 'nv', command: 'node' } + * ``` + * + * @example + * ```typescript + * resolveAlias(`alias nv=node`) // { aliasName: 'nv', command: 'node' } + * ``` + * * @param {value} string * @returns If it is not valid, return undefined */ @@ -108,7 +125,18 @@ export function resolveAlias(value: string): Pick node + if ( + allNotEqualToTarget([firstCommandChar, lastCommandChar], `'`) && + allNotEqualToTarget([firstCommandChar, lastCommandChar], `"`) + ) { + command = command.split(' ')[0]; + } + + if (isValid(aliasName) && isValid(command)) { return { aliasName: aliasName.replace(/^['"](.*)['"]$/, '$1'), command: command.replace(/^['"](.*)['"]$/, '$1'), @@ -129,7 +157,7 @@ export function isSameAlias(targetAlias: Alias, sourceAlias: Alias) { } /** - * covert value to array + * Covert value to array * @param {Array | undefined} value * @returns {boolean} */ @@ -138,7 +166,7 @@ export function normalizeAliasesToArray(value: T[] | undefined) { } /** - * generate unalias command + * Generate unalias command * @param {Alias[]} aliases * @returns {string} */ diff --git a/tests/utils.spec.ts b/tests/utils.spec.ts index 90249ac..be441a8 100644 --- a/tests/utils.spec.ts +++ b/tests/utils.spec.ts @@ -1,6 +1,13 @@ import { describe, expect, it } from 'vitest'; import type { Alias } from '../src/types'; -import { formatUnaliasCommand, isSameAlias, normalizeAliasesToArray, resolveAlias } from '../src/utils'; +import { + allNotEqualToTarget, + formatUnaliasCommand, + isSameAlias, + isValid, + normalizeAliasesToArray, + resolveAlias, +} from '../src/utils'; const alias = { aliasName: 'nv', @@ -43,11 +50,6 @@ describe('test alias resolve', () => { expect(resolveAlias(value)).toStrictEqual(alias); }); - it('alias command without quote', () => { - const value = `alias "nv"=node -v`; - expect(resolveAlias(value)).toStrictEqual(undefined); - }); - it('space before =', () => { const value = `alias nv ='node -v'`; expect(resolveAlias(value)).toStrictEqual(undefined); @@ -57,6 +59,22 @@ describe('test alias resolve', () => { const value = `alias nv= 'node -v'`; expect(resolveAlias(value)).toStrictEqual(undefined); }); + + it('alias name without quote and command has space without quote', () => { + const value = 'alias nv=node -v'; + expect(resolveAlias(value)).toStrictEqual({ + aliasName: alias.aliasName, + command: 'node', + }); + }); + + it('alias name without quote and command without space and quote', () => { + const value = 'alias nv=node'; + expect(resolveAlias(value)).toStrictEqual({ + aliasName: alias.aliasName, + command: 'node', + }); + }); }); describe('test same alias', () => { @@ -108,3 +126,27 @@ describe('test format unalias command', () => { expect(formatUnaliasCommand([alias, alias])).toBe('unalias nv nv'); }); }); + +describe('test alias name and command are valid', () => { + it('param without quote', () => { + expect(isValid('test')).toBe(true); + }); + + it('param without quote', () => { + expect(isValid(`'test'`)).toBe(true); + }); + + it('param without quote', () => { + expect(isValid(`"test"`)).toBe(true); + }); +}); + +describe('test all values are not equal to target', () => { + it('param are same to target', () => { + expect(allNotEqualToTarget(['1', '1'], '2')).toBe(true); + }); + + it('param are not same to target', () => { + expect(allNotEqualToTarget(['2', '1'], '2')).toBe(false); + }); +});