Skip to content

Commit

Permalink
hash parser function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Nov 16, 2023
1 parent ca3341e commit 4d8ea0c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/formatters/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function starts_with(match: MatchResultT): match is NonNullable<MatchResultT> {
// :: get_next_character function only cmd in input use original
// :: not optimized function
// -------------------------------------------------------------------------
function make_next_char_fun(string: string) {
export function make_next_char_fun(string: string) {
const tests: Array<(arg: string) => string | void> = [];
[
entity_re,
Expand Down Expand Up @@ -172,12 +172,29 @@ export function escape_brackets(string: string) {
.replace(/\\/g, '&#92;');
}

type FormatterFunctionOptions = {
echo: boolean;
animation: boolean;
prompt: boolean;
command: boolean;
position: number;
const local_format_parts_re = new RegExp(format_parts_re.source, 'i'); // without g flag

export function count_selfclosing_formatting(string: string) {
let count = 0;
if (have_formatting(string)) {
format_split(string).forEach(function(str) {
if (is_formatting(str)) {
const m = str.match(local_format_parts_re);
if (m && m[1].match(/@/) && m[6] === '') {
count++;
}
}
});
}
return count;
}

export type FormatterFunctionOptions = {
echo?: boolean;
animation?: boolean;
prompt?: boolean;
command?: boolean;
position?: number;
};

export type FormatterRegExpFunction = (...args: string[]) => string;
Expand All @@ -198,3 +215,7 @@ type FormatterArrayOptions = {
};

export type Formatter = [RegExp, FormaterRegExpReplacement] | [RegExp, FormaterRegExpReplacement, FormatterArrayOptions] | FormatterFunction;

export function is_formatter_function(formatter: Formatter): formatter is FormatterFunction {
return typeof formatter === 'function';
}
31 changes: 31 additions & 0 deletions src/guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function is_string(arg: unknown): arg is string {
return typeof arg === 'string';
}

export function is_function(arg: unknown): arg is () => unknown {
return typeof arg === 'function';
}

export function is_array(arg: unknown): arg is Array<unknown> {
return Array.isArray(arg);
}

export function is_integer(arg: unknown): arg is number {
return Number.isInteger(arg);
}

export type Hash_Command = [number, number, string];

export function is_hash_command(arg: unknown): arg is Hash_Command {
if (!is_array(arg)) {
return false;
}
if (arg.length !== 3) {
return false;
}
if (!(is_integer(arg[0]) && is_integer(arg[1]))) {
return false;
}
return is_string(arg[2]);

}
18 changes: 18 additions & 0 deletions src/parser/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { is_array, is_hash_command, type Hash_Command} from '../guards';

export function parse_hash(hash: string): Array<Hash_Command> | never {
let input;
try {
input = JSON.parse(hash);
} catch (e) {
throw new Error(`invalid hash ${hash}`);
}
if (!is_array(input)) {
throw new Error(`invalid hash ${hash}`);
}
const valid = input.every(is_hash_command);
if (!valid) {
throw new Error(`invalid hash ${hash}`);
}
return input as Array<Hash_Command>;
}

0 comments on commit 4d8ea0c

Please sign in to comment.