Skip to content

Commit

Permalink
fix: casing fn handles leading char (#1256)
Browse files Browse the repository at this point in the history
* fix: casing fn handles leading char

* style: use index

* Update src/functions/casing.ts

* Update src/functions/casing.ts

Co-authored-by: nulltoken <emeric.fermas@gmail.com>

Co-authored-by: nulltoken <emeric.fermas@gmail.com>
  • Loading branch information
P0lip and nulltoken authored Jun 26, 2020
1 parent ff79fcf commit 23e407a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/functions/__tests__/casing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ describe('casing', () => {
);
});

test.each(Object.values(CasingType))('properly detects leading char for %s casing', casing => {
expect(runCasing('/', casing, true, { allowLeading: true, char: '/' })).toBeUndefined();
expect(runCasing('//', casing, true, { allowLeading: true, char: '/' })).toEqual([
{ message: `must be ${casing} case` },
]);
});

test('allows advanced scenarios', () => {
expect(runCasing('X-MyAmazing-Header', CasingType.pascal, true, { char: '-' })).toBeUndefined();
expect(
Expand Down
9 changes: 9 additions & 0 deletions src/functions/casing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export const casing: IFunction<ICasingOptions> = (targetVal, opts) => {
return;
}

if (
targetVal.length === 1 &&
opts.separator !== void 0 &&
opts.separator.allowLeading === true &&
targetVal === opts.separator.char
) {
return;
}

const casingValidator = buildFrom(CASES[opts.type], opts);

if (casingValidator.test(targetVal)) {
Expand Down

0 comments on commit 23e407a

Please sign in to comment.