From 4c5367a44bb6af6429922ccb55a95ec687f8ead9 Mon Sep 17 00:00:00 2001 From: Jayk <20700514+jayk09@users.noreply.github.com> Date: Fri, 4 Feb 2022 22:55:34 +0100 Subject: [PATCH] Add parser utils, refactor --- src/index.ts | 2 +- src/parser/{parser.ts => index.ts} | 2 +- src/parser/modifiers.ts | 2 +- src/parser/utils.ts | 5 +++++ src/utils.ts | 7 +++---- 5 files changed, 11 insertions(+), 7 deletions(-) rename src/parser/{parser.ts => index.ts} (98%) create mode 100644 src/parser/utils.ts diff --git a/src/index.ts b/src/index.ts index a30111f..7883211 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { derived, get, writable } from 'svelte/store'; import { fetchTranslations, testRoute, toDotNotation, useDefault as d } from './utils'; -import parser from './parser/parser'; +import parser from './parser'; import type { Config, ConfigTranslations, LoaderModule, LoadingStore, LocalTranslationFunction, Route, TranslationFunction, Translations, ExtendedStore } from './types'; import type { CustomModifiers } from './parser/types'; diff --git a/src/parser/parser.ts b/src/parser/index.ts similarity index 98% rename from src/parser/parser.ts rename to src/parser/index.ts index a82b51b..2f25ec7 100644 --- a/src/parser/parser.ts +++ b/src/parser/index.ts @@ -1,5 +1,5 @@ import * as defaultModifiers from './modifiers'; -import { useDefault } from '../utils'; +import { useDefault } from './utils'; import type { CustomModifiers, ModifierOption, Parser } from './types'; diff --git a/src/parser/modifiers.ts b/src/parser/modifiers.ts index 1ca3a78..08dfe19 100644 --- a/src/parser/modifiers.ts +++ b/src/parser/modifiers.ts @@ -1,4 +1,4 @@ -import { useDefault, findOption } from '../utils'; +import { useDefault, findOption } from './utils'; import type { Modifier, ModifierOption } from './types'; export const eq: Modifier = (value, options = [], defaultValue = '') => useDefault(options.find( diff --git a/src/parser/utils.ts b/src/parser/utils.ts new file mode 100644 index 0000000..34c72f1 --- /dev/null +++ b/src/parser/utils.ts @@ -0,0 +1,5 @@ +import type { ModifierOption } from './types'; + +export const useDefault = (value: any, def:any = {}): T => value || def; + +export const findOption = (options: ModifierOption[], key: string, defaultValue?: string): T => ((options.find((option) => option.key === key))?.value || defaultValue) as any; diff --git a/src/utils.ts b/src/utils.ts index fc9af68..bd75858 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,8 @@ +import { useDefault } from './parser/utils'; + import type { ToDotNotation, FetchTranslations, Route, LoaderModule } from './types'; -import type { ModifierOption } from './parser/types'; -export const useDefault = (value: any, def:any = {}): T => value || def; +export { useDefault }; export const toDotNotation: ToDotNotation = (input, parentKey) => Object.keys(useDefault(input)).reduce((acc, key) => { const value = input[key]; @@ -46,5 +47,3 @@ export const testRoute = (route: string) => (input: Route) => { return false; }; - -export const findOption = (options: ModifierOption[], key: string, defaultValue?: string): T => ((options.find((option) => option.key === key))?.value || defaultValue) as any;