1 | 'use strict';
|
2 |
|
3 | const flattenArray = (array = [], result = []) => {
|
4 | for (let i = 0; i < array.length; i++) {
|
5 | const value = array[i];
|
6 |
|
7 | if (Array.isArray(value))
|
8 | flattenArray(value, result);
|
9 | else
|
10 | result.push(value);
|
11 | }
|
12 |
|
13 | return result;
|
14 | };
|
15 |
|
16 | const flatten = (array = []) => {
|
17 | if (!Array.isArray(array))
|
18 | array = [array];
|
19 | return flattenArray(array);
|
20 | };
|
21 |
|
22 | class Lazy {
|
23 | constructor(factory) {
|
24 | this.factory = factory;
|
25 | this.instance = null;
|
26 | }
|
27 |
|
28 | get created() {
|
29 | return !!this.instance;
|
30 | }
|
31 |
|
32 | ifCreated(fn = () => {}) {
|
33 | if (this.instance)
|
34 | return fn(this.instance);
|
35 | return null;
|
36 | }
|
37 |
|
38 | get() {
|
39 | if (!this.instance)
|
40 | this.instance = this.factory();
|
41 | return this.instance;
|
42 | }
|
43 | }
|
44 |
|
45 | module.exports = {
|
46 | Lazy,
|
47 | flatten,
|
48 | };
|