Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit 37de44e

Browse files
committed
feat: init utils func
1 parent 1fa43d1 commit 37de44e

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
const unm: string = '1'
1+
import * as Check from './utils/check'
2+
import { LoaderOptions } from './types'
23

3-
console.log(unm)
4+
export class BackLoader {
5+
6+
constructor(ops: LoaderOptions) {
7+
Check.options(ops)
8+
}
9+
10+
11+
}

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
export type LoaderOptions = {
3+
pages: string[]
4+
scripts: string[]
5+
}
6+

src/utils/check.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as Log from './log'
2+
import { LoaderOptions } from '../types'
3+
4+
const regs = {
5+
page: /^http|^\/\//,
6+
js: /(^http|^\/\/)\S+\.js$/,
7+
}
8+
9+
export const pages = (pages: string[]): boolean => {
10+
if (!Array.isArray(pages)) return Log.options.pagesError()
11+
const unnormalLinks = pages.filter(page => !regs.page.test(page))
12+
if (unnormalLinks && unnormalLinks.length) return Log.options.pagesError()
13+
return true
14+
}
15+
16+
export const scripts = (scripts: string[]): boolean => {
17+
if (!Array.isArray(scripts)) return Log.options.scriptsError()
18+
const unnormalLinks = scripts.filter(s => !regs.js.test(s))
19+
if (unnormalLinks && unnormalLinks.length) return Log.options.scriptsError()
20+
return true
21+
}
22+
23+
24+
export const options = (ops: LoaderOptions): boolean => {
25+
const checkResults: boolean[] = []
26+
ops.pages && checkResults.push(pages(ops.pages))
27+
ops.scripts && checkResults.push(scripts(ops.scripts))
28+
29+
if (!checkResults.length) return Log.options.isEmpty()
30+
return !`${checkResults}`.includes('false')
31+
}
32+

src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

src/utils/log.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
export const warning = (msg: string) => {
3+
console.warn(`BackLoader: ${msg}`)
4+
return false
5+
}
6+
7+
export const options = {
8+
pagesError: () => warning('options [pages] error.'),
9+
scriptsError: () => warning('options [scripts] error.'),
10+
isEmpty: () => warning('options is empty!'),
11+
}

tests/utils/check.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)