Skip to content

Commit

Permalink
feat: add comparePaths function
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Jun 6, 2023
1 parent ca8dfca commit ecc4f51
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions dictionaries/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ pinst
iteratable
FMRADIO
vitest
réservé
29 changes: 29 additions & 0 deletions src/string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';

import {
comparePaths,
crlfToLf,
getInitials,
isNullOrEmpty,
Expand Down Expand Up @@ -294,4 +295,32 @@ describe('string', () => {
},
);
});

describe('comparePaths', () => {
it('should return 0 if paths are the same', () => {
expect(comparePaths('/path1/', '/path1/')).toBe(0);
});

it('should return 1 if path1 is bigger', () => {
expect(comparePaths('b', 'a')).toBe(1);
});

it('should return 0 if path1 is equal to path2 but with different case', () => {
expect(comparePaths('path', 'PAth')).toBe(0);
});

it('should return non-zero if path1 is equal to path2 but with different accent', () => {
expect(comparePaths('reserve', 'réservé')).not.toBe(0);
});

it('should ignore trailing slashes', () => {
expect(comparePaths('reserve', 'reserve//')).toBe(0);
expect(comparePaths('reserve//', 'reserve/')).toBe(0);
});

it('should ignore leading slashes', () => {
expect(comparePaths('//reserve', 'reserve')).toBe(0);
expect(comparePaths('/reserve', '//reserve')).toBe(0);
});
});
});
24 changes: 21 additions & 3 deletions src/string.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNull, isString } from './guards.js';
import { isWordOrWords } from './internals/string.js';
import { hasInvalidCasing, tokenize } from './internals/tokenize.js';
import type { Nullish } from './types.js';
import type { Nullish, Sensitivity } from './types.js';

export const toSpaceCase = tokenize;

Expand Down Expand Up @@ -34,7 +34,7 @@ export const toSnakeCase = (str: string) =>
? str
: toSpaceCase(str).replace(
/(\s[a-z])/g,
(_, letter) => `_${letter.slice(1)}`,
(_, letter: string) => `_${letter.slice(1)}`,
);

/**
Expand All @@ -45,7 +45,7 @@ export const toKebabCase = (str: string) =>
? str
: toSpaceCase(str).replace(
/(\s[a-z])/g,
(_, letter) => `-${letter.slice(1)}`,
(_, letter: string) => `-${letter.slice(1)}`,
);

/**
Expand Down Expand Up @@ -99,3 +99,21 @@ export const joinPath = (...paths: string[]): string => {
);
return result.join('/');
};

/**
* Compare two path without trailing and leading slashes
* @param {string} path1: first path
* @param {string} path2: second path
* @return {boolean} is they are equal
*/
export const comparePaths = (
path1: string,
path2: string,
{ sensitivity = 'accent' }: { sensitivity?: Sensitivity } = {},
): number => {
const normalPath1 = removeLeadingSlashes(removeTrailingSlashes(path1));
const normalPath2 = removeLeadingSlashes(removeTrailingSlashes(path2));
return normalPath1.localeCompare(normalPath2, undefined, {
sensitivity,
});
};
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ export type Tuple<T, N extends number> = N extends N
? T[]
: TupleOf<T, N, []>
: never;

export type Sensitivity = 'accent' | 'base' | 'case' | 'variant';

0 comments on commit ecc4f51

Please sign in to comment.