Skip to content

Commit

Permalink
enhanced script-file-export loader
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 7, 2022
1 parent e3ee1c5 commit 50d27e4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
1 change: 0 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[![npm version](https://badge.fury.io/js/locter.svg)](https://badge.fury.io/js/locter)
[![codecov](https://codecov.io/gh/Tada5hi/locter/branch/master/graph/badge.svg?token=4KNSG8L13V)](https://codecov.io/gh/Tada5hi/locter)
[![CI](https://github.com/tada5hi/locter/actions/workflows/main.yml/badge.svg)](https://github.com/tada5hi/locter/actions/workflows/main.yml)
[![Known Vulnerabilities](https://snyk.io/test/github/Tada5hi/locter/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Tada5hi/locter?targetFile=package.json)

# Locter 🔥
Locter is a library to locate and load a file regarding specific criteria.
Expand Down
16 changes: 8 additions & 8 deletions src/loader/file-type/script/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* view the LICENSE file that was distributed with this source code.
*/

import {getRecordItem} from "./utils";
import {LoaderFilterFn} from "./type";
import {isLocatorInfo, LocatorInfo} from "../../../locator";
import {buildLoaderFilePath} from "../../utils";
import { getExportItem } from './utils';
import { LoaderFilterFn, ScriptFileExportItem } from './type';
import { LocatorInfo, isLocatorInfo } from '../../../locator';
import { buildLoaderFilePath } from '../../utils';

export async function loadScriptFile(data: LocatorInfo |string) : Promise<unknown | undefined> {
export async function loadScriptFile(data: LocatorInfo | string) : Promise<unknown | undefined> {
const filePath = isLocatorInfo(data) ?
buildLoaderFilePath(data) :
data;
Expand All @@ -26,17 +26,17 @@ export async function loadScriptFile(data: LocatorInfo |string) : Promise<unknow
export async function loadScriptFileExport(
data: LocatorInfo | string,
filterFn?: LoaderFilterFn,
) : Promise<unknown | undefined> {
) : Promise<ScriptFileExportItem | undefined> {
const filePath = isLocatorInfo(data) ?
buildLoaderFilePath(data) :
data;

try {
const data = await loadScriptFile(filePath);

return getRecordItem(data, filterFn);
return getExportItem(data, filterFn);
} catch (e) {
/* istanbul ignore next */
return undefined
return undefined;
}
}
14 changes: 7 additions & 7 deletions src/loader/file-type/script/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* view the LICENSE file that was distributed with this source code.
*/

import {LoaderFilterFn} from "./type";
import {getRecordItem} from "./utils";
import {isLocatorInfo, LocatorInfo} from "../../../locator";
import {buildLoaderFilePath} from "../../utils";
import { LoaderFilterFn, ScriptFileExportItem } from './type';
import { getExportItem } from './utils';
import { LocatorInfo, isLocatorInfo } from '../../../locator';
import { buildLoaderFilePath } from '../../utils';

export function loadScriptFileSync(data: LocatorInfo | string) : unknown | undefined {
const filePath = isLocatorInfo(data) ?
Expand All @@ -27,17 +27,17 @@ export function loadScriptFileSync(data: LocatorInfo | string) : unknown | undef
export function loadScriptFileExportSync(
data: LocatorInfo | string,
filterFn?: LoaderFilterFn,
) : unknown | undefined {
) : ScriptFileExportItem | undefined {
const filePath = isLocatorInfo(data) ?
buildLoaderFilePath(data) :
data;

try {
const data = loadScriptFileSync(filePath);

return getRecordItem(data, filterFn);
return getExportItem(data, filterFn);
} catch (e) {
/* istanbul ignore next */
return undefined
return undefined;
}
}
4 changes: 4 additions & 0 deletions src/loader/file-type/script/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
*/

export type LoaderFilterFn = (key: string, value: unknown) => boolean;
export type ScriptFileExportItem = {
key: string,
value: unknown
};
22 changes: 13 additions & 9 deletions src/loader/file-type/script/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
* view the LICENSE file that was distributed with this source code.
*/

import {hasOwnProperty} from "../../../utils";
import {LoaderFilterFn} from "./type";
import { hasOwnProperty } from '../../../utils';
import { LoaderFilterFn, ScriptFileExportItem } from './type';

export function getRecordItem(
export function getExportItem(
data: Record<string, any>,
filterFn: LoaderFilterFn
) : unknown | undefined {
filterFn: LoaderFilterFn,
) : ScriptFileExportItem | undefined {
if (filterFn) {
const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
if (filterFn(keys[i], data[keys[i]])) {
return data[keys[i]];
return {
key: keys[i],
value: data[keys[i]],
};
}
}
} else {
return hasOwnProperty(data, 'default') ?
data.default :
data;
return {
key: 'default',
value: hasOwnProperty(data, 'default') ? data.default : data,
};
}

/* istanbul ignore next */
Expand Down
19 changes: 12 additions & 7 deletions test/unit/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('src/loader/**', () => {

loaderContent = await loadScriptFileExport(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.foo).toEqual('bar');
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({foo: 'bar'});

// --------------------------------------------------------------------

Expand All @@ -34,8 +35,8 @@ describe('src/loader/**', () => {

loaderContent = loadScriptFileExportSync(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.default).toBeUndefined();
expect(loaderContent.foo).toEqual('bar');
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({foo: 'bar'});
});

it('should load .ts file', async () => {
Expand All @@ -47,7 +48,8 @@ describe('src/loader/**', () => {

loaderContent = await loadScriptFileExport(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.bar).toEqual('baz');
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({bar: 'baz'});

// --------------------------------------------------------------------

Expand All @@ -58,7 +60,8 @@ describe('src/loader/**', () => {

loaderContent = loadScriptFileExportSync(locatorInfo);
expect(loaderContent).toBeDefined();
expect(loaderContent.bar).toEqual('baz');
expect(loaderContent.key).toEqual('default');
expect(loaderContent.value).toEqual({bar: 'baz'});
});

it('should filter .ts file', async () => {
Expand All @@ -67,14 +70,16 @@ describe('src/loader/**', () => {
return key === 'bar';
});
expect(loaderContent).toBeDefined();
expect(loaderContent).toEqual('baz');
expect(loaderContent.key).toEqual('bar');
expect(loaderContent.value).toEqual('baz');

locatorInfo = locateFileSync( 'file-many-ts', {paths: [basePath], extensions: ['.ts']});
loaderContent = loadScriptFileExportSync(locatorInfo, (key, value) => {
return key === 'bar';
});
expect(loaderContent).toBeDefined();
expect(loaderContent).toEqual('baz');
expect(loaderContent.key).toEqual('bar');
expect(loaderContent.value).toEqual('baz');
})

it('should load .json file', async () => {
Expand Down

0 comments on commit 50d27e4

Please sign in to comment.