Skip to content

Commit

Permalink
Add parser utils, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jarda-svoboda committed Feb 4, 2022
1 parent 8813ca7 commit 4c5367a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parser.ts → src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as defaultModifiers from './modifiers';
import { useDefault } from '../utils';
import { useDefault } from './utils';

import type { CustomModifiers, ModifierOption, Parser } from './types';

Expand Down
2 changes: 1 addition & 1 deletion src/parser/modifiers.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
5 changes: 5 additions & 0 deletions src/parser/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ModifierOption } from './types';

export const useDefault = <T = any>(value: any, def:any = {}): T => value || def;

export const findOption = <T = string>(options: ModifierOption[], key: string, defaultValue?: string): T => ((options.find((option) => option.key === key))?.value || defaultValue) as any;
7 changes: 3 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = <T = any>(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];
Expand Down Expand Up @@ -46,5 +47,3 @@ export const testRoute = (route: string) => (input: Route) => {

return false;
};

export const findOption = <T = string>(options: ModifierOption[], key: string, defaultValue?: string): T => ((options.find((option) => option.key === key))?.value || defaultValue) as any;

0 comments on commit 4c5367a

Please sign in to comment.