Passed
Push — master ( 4f0cd7...531117 )
by Andrii
03:12
created

src/utils.ts   A

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 36
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 5
mnd 2
bc 2
fnc 3
bpm 0.6666
cpm 1.6666
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A utils.ts ➔ regexpize 0 5 2
A utils.ts ➔ readlineSync 0 4 1
A utils.ts ➔ extractDefaults 0 13 2
1
import {readFileSync} from "fs"
2
import type { SchemaWithDefaultsAndExamples, DefaultsAndExamplesFromSchema } from "./ts-swiss.types"
3
4
const {keys: $keys} = Object
5
6
export {
7
  regexpize,
8
  extractDefaults,
9
  readlineSync
10
}
11
12
function regexpize(source: string|RegExp, flags = "") {
13
  return typeof source === "string"
14
  ? new RegExp(source, flags)
15
  : source
16
}
17
18
function extractDefaults<S extends SchemaWithDefaultsAndExamples>({properties}: S) {
19
  const keys: Array<keyof typeof properties> = $keys(properties)
20
  , {length} = keys
21
  , defaults: Partial<DefaultsAndExamplesFromSchema<S>> = {}
22
23
  for (let i = length; i--; ) {
24
    const key = keys[i]
25
    //@ts-ignore
26
    defaults[key] = properties[key].default
27
  }
28
29
  return defaults as DefaultsAndExamplesFromSchema<S>
30
}
31
32
//TODO replace with common
33
function readlineSync(path: string, splitter: string) {
34
  return readFileSync(path).toString().split(splitter)
35
}
36