Skip to content

Commit

Permalink
feat: added stripParenthesis(val) utility
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeeinlondon committed Nov 7, 2024
1 parent a96c31c commit 45c989d
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 55 deletions.
30 changes: 6 additions & 24 deletions src/runtime/errors/KindError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toKebabCase, toPascalCase } from "../literals";
import { stripChars, toKebabCase, toPascalCase } from "../literals";
import {
AfterFirst,
As,
Expand All @@ -10,29 +10,14 @@ import {
KindError,
KindErrorDefn,
MergeObjects,
Narrowable
Narrowable,
StripChars
} from "inferred-types/dist/types/index";

import { parse } from "error-stack-parser-es/lite";
import { relative } from "pathe";

type Merged<
TKeys extends readonly string[],
TBase extends Record<string, unknown>,
TErr extends Record<string, unknown>,
TResult extends Record<string, unknown> = EmptyObject
> = [] extends TKeys
? ExpandDictionary<TResult>
: Merged<
AfterFirst<TKeys>,
TBase,
TErr,
First<TKeys> extends keyof TErr
? TResult & Record<First<TKeys>, TErr[First<TKeys>]>
: First<TKeys> extends keyof TBase
? TResult & Record<First<TKeys>, TBase[First<TKeys>]>
: never
>;



const IGNORABLES = [
Expand Down Expand Up @@ -86,10 +71,7 @@ export function kindError<
context: TErrContext = {} as EmptyObject as TErrContext
): KindError<
TKind,
Merged<
As<CombinedKeys<TBaseContext,TErrContext>, readonly string[]>,
TBaseContext,TErrContext
>
MergeObjects<TBaseContext,TErrContext>
> => {
const err = new Error(msg) as Partial<
KindError<
Expand All @@ -106,7 +88,7 @@ export function kindError<
}));

err.name = toPascalCase(kind);
err.kind = toKebabCase(kind);
err.kind = toKebabCase(stripChars(kind, "<",">", "[", "]", "(",")"));
err.file = stackTrace[0].file;
err.line = stackTrace[0].line;
err.col = stackTrace[0].col;
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/literals/stripChars.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StripChars, TupleToUnion } from "inferred-types/dist/types/index";
import { asChars } from "src/runtime/index";
import { asChars } from "inferred-types/dist/runtime/index";

/**
* **stripChars**`(content, ...strip)`
Expand All @@ -14,12 +14,12 @@ export const stripChars = <
>(
content: TContent,
...strip: TRetain
): StripChars<TContent, TupleToUnion<TRetain>> => {
): StripChars<TContent, TRetain[number]> => {
let chars: readonly string[] = asChars(content);

return (
chars.filter(c => !strip.includes(c)).join("")
) as unknown as StripChars<TContent, TupleToUnion<TRetain>>
) as unknown as StripChars<TContent, TRetain[number]>
}


5 changes: 4 additions & 1 deletion src/runtime/literals/toKebabCase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { KebabCase } from "inferred-types/dist/types/index";




/**
* **toKebabCase**(str)
*
Expand All @@ -13,7 +16,7 @@ import { KebabCase } from "inferred-types/dist/types/index";
export function toKebabCase<
S extends string,
P extends boolean = false
>(input: S, _preserveWhitespace?: P) {
>(input: S, preserveWhitespace: P = false as P) {
const [_, preWhite, focus, postWhite] = /^(\s*)(.*?)(\s*)$/.exec(input) as RegExpExecArray;

const replaceWhitespace = (i: string) => i.replace(/\s/gs, "-");
Expand Down
1 change: 1 addition & 0 deletions src/runtime/type-conversion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export * from "./ip6GroupExpansion";
export * from "./csv";
export * from "./lookupCountry";
export * from "./json";
export * from "./stripParenthesis";

15 changes: 15 additions & 0 deletions src/runtime/type-conversion/stripParenthesis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { stripLeading, stripTrailing } from "inferred-types/dist/runtime/index"
import { StripSurround, Trim } from "inferred-types/dist/types/index"


/**
* **stripParenthesis**`(val)`
*
* A runtime utility which strips leading and trailing whitespace as well
* as any leading or trailing parenthesis characters.
*/
export const stripParenthesis = <
T extends string
>(val: T) => {
return stripTrailing(stripLeading(val.trim(), "("), ")").trim() as unknown as Trim<StripSurround<Trim<T>, "("|")">>
}
5 changes: 3 additions & 2 deletions src/types/errors/KindError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
KebabCase,
EmptyObject,
Narrowable,
MergeObjects
MergeObjects,
StripChars
} from "inferred-types/dist/types/index";
import { StackFrame } from "error-stack-parser-es";

Expand Down Expand Up @@ -37,7 +38,7 @@ export interface KindError<
> extends Error {
__kind: "KindError";
name: PascalCase<TKind>;
kind: KebabCase<TKind>;
kind: KebabCase<StripChars<TKind, "<"|">"| "["| "]"| "("|")">>;
file?: string;
line?: number;
col?: number;
Expand Down
45 changes: 26 additions & 19 deletions src/types/string-literals/casing/KebabCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import {
} from "inferred-types/dist/types/index"


type Process<
TString extends string,
TPreserve extends boolean = false
> = TPreserve extends true
? // preserve
Concat<[
LeftWhitespace<TString>,
KebabCase<TString, false>,
RightWhitespace<TString>
]>

: // remove whitespace
string extends TString
? string
: DashUppercase<Trim<LowerAllCaps<TString>>> extends `${infer Begin}${"_" | " "}${infer Rest}`
? KebabCase<`${Lowercase<Begin>}-${Rest}`>
: Replace<
Lowercase<
DashUppercase<Uncapitalize<Trim<LowerAllCaps<TString>>>>
>,
"--", "-"
>;

/**
* **KebabCase**`<TString,TPreserve>`
*
Expand All @@ -27,22 +50,6 @@ import {
export type KebabCase<
TString extends string,
TPreserve extends boolean = false
> = TPreserve extends true
? // preserve
Concat<[
LeftWhitespace<TString>,
KebabCase<TString, false>,
RightWhitespace<TString>
]>

: // remove whitespace
string extends TString
? string
: DashUppercase<Trim<LowerAllCaps<TString>>> extends `${infer Begin}${"_" | " "}${infer Rest}`
? KebabCase<`${Lowercase<Begin>}-${Rest}`>
: Replace<
Lowercase<
DashUppercase<Uncapitalize<Trim<LowerAllCaps<TString>>>>
>,
"--", "-"
>;
> = Process<TString, TPreserve> extends string
? Process<TString, TPreserve>
: never;
6 changes: 5 additions & 1 deletion src/types/type-conversion/StripChars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ export type StripChars<
> = Or<[IsWideType<TContent>, IsWideType<TStrip>]> extends true
? string
: // both TContent and TStrip are literals
Concat<Process<Chars<TContent>, TStrip>>;
Chars<TContent> extends readonly string[]
? Concat<Process<Chars<TContent>, TStrip>> extends string
? Concat<Process<Chars<TContent>, TStrip>>
: never
: never;

7 changes: 5 additions & 2 deletions tests/errors/KindError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,16 @@ describe("KindError", () => {


it("with awkward name", () => {
const err = kindError("FooBar<12>");
const err = kindError("FooBar<Baz>");
const fooBarBaz = err("well, well");

expect(fooBarBaz.name).toEqual("FooBar<Baz>");
expect(fooBarBaz.kind).toEqual("foo-bar-baz");


// @ts-ignore
type cases = [
/** type tests */
Expect<Equal<typeof fooBarBaz, KindError<"FooBar<Baz>"> >>
];

});
Expand Down
7 changes: 4 additions & 3 deletions tests/string-literals/stripChars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ describe("stripChars(content,...strip)", () => {

it("happy path", () => {
const lower = stripChars("FooBar", ...UPPER_ALPHA_CHARS);
const special = stripChars("FooBar<Baz>", "<",">", "[", "]", "(",")");

expect(lower).toBe("ooar");
expect(special).toBe("FooBarBaz");

// @ts-ignore
type cases = [
Expect<Equal<typeof lower, "ooar">>,
];
const cases: cases = [
true
Expect<Equal<typeof special, "FooBarBaz">>,
];
});

Expand Down
29 changes: 29 additions & 0 deletions tests/type-conversions/stripParenthesis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Equal, Expect } from "@type-challenges/utils";
import { describe, expect, it } from "vitest";
import { stripParenthesis } from "inferred-types";

// Note: while type tests clearly fail visible inspection, they pass from Vitest
// standpoint so always be sure to run `tsc --noEmit` over your test files to
// gain validation that no new type vulnerabilities have cropped up.

describe("stripParenthesis(val)", () => {

it("happy path", () => {
const t1 = stripParenthesis("foo bar");
const t2 = stripParenthesis("(foo bar)");
const t3 = stripParenthesis(" (foo bar) ");

expect(t1).toEqual("foo bar");
expect(t2).toEqual("foo bar");
expect(t3).toEqual("foo bar");


// @ts-ignore
type cases = [
Expect<Equal<typeof t1, "foo bar">>,
Expect<Equal<typeof t2, "foo bar">>,
Expect<Equal<typeof t3, "foo bar">>,
];
});

});

0 comments on commit 45c989d

Please sign in to comment.