Skip to content

Commit

Permalink
feat: support setting command without quote (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouji authored Dec 6, 2024
1 parent 4911ad7 commit fde5592
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
40 changes: 34 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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<T>(values: T[], target: T) {
return values.every((value) => value !== target);
}

/**
* Check alias name or command is valid
*
Expand All @@ -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);

Expand All @@ -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;
}

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -108,7 +125,18 @@ export function resolveAlias(value: string): Pick<Alias, 'aliasName' | 'command'
return;
}

if (isValid(aliasName) && isValid(command, true)) {
const firstCommandChar = command.charAt(0);
const lastCommandChar = command.charAt(command.length - 1);

// clear command if command isn't wrapper by quote, e.g. node -v -> 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'),
Expand All @@ -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}
*/
Expand All @@ -138,7 +166,7 @@ export function normalizeAliasesToArray<T>(value: T[] | undefined) {
}

/**
* generate unalias command
* Generate unalias command
* @param {Alias[]} aliases
* @returns {string}
*/
Expand Down
54 changes: 48 additions & 6 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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);
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit fde5592

Please sign in to comment.