UNPKG

3.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const fs_1 = require("fs");
5const glob_1 = tslib_1.__importDefault(require("glob"));
6const path_1 = require("path");
7const tinspector_1 = require("tinspector");
8String.prototype.format = function (...args) {
9 return this.replace(/{(\d+)}/g, (m, i) => typeof args[i] != 'undefined' ? args[i] : m);
10};
11Array.prototype.flatten = function () {
12 return this.reduce((a, b) => a.concat(b), []);
13};
14function hasKeyOf(opt, key) {
15 return key in opt;
16}
17exports.hasKeyOf = hasKeyOf;
18function isCustomClass(type) {
19 switch (Array.isArray(type) ? type[0] : type) {
20 case undefined:
21 case Boolean:
22 case String:
23 case Array:
24 case Number:
25 case Object:
26 case Date:
27 return false;
28 default:
29 return true;
30 }
31}
32exports.isCustomClass = isCustomClass;
33function resolvePath(path) {
34 const removeExtension = (x) => x.replace(/\.[^/.]+$/, "");
35 if (fs_1.existsSync(`${path}.js`))
36 return [removeExtension(path)];
37 else if (fs_1.existsSync(`${path}.ts`))
38 return [removeExtension(path)];
39 //resolve provided path directory or file
40 else if (fs_1.lstatSync(path).isDirectory()) {
41 const files = glob_1.default.sync(`${path}/**/*+(.js|.ts)`)
42 //take only file in extension list
43 .filter(x => [".js", ".ts"].some(ext => path_1.extname(x) == ext))
44 //add root path + file name
45 .map(x => removeExtension(x));
46 return Array.from(new Set(files));
47 }
48 else
49 return [path];
50}
51exports.resolvePath = resolvePath;
52function reflectPath(path) {
53 if (Array.isArray(path))
54 return path.map(x => tinspector_1.reflect(x));
55 else if (typeof path === "string")
56 return resolvePath(path)
57 .map(x => tinspector_1.reflect(x))
58 .map(x => x.members)
59 .flatten();
60 else
61 return [tinspector_1.reflect(path)];
62}
63exports.reflectPath = reflectPath;
64const log = console.log;
65var consoleLog;
66(function (consoleLog) {
67 function startMock() {
68 console.log = jest.fn(message => { });
69 }
70 consoleLog.startMock = startMock;
71 function clearMock() {
72 console.log = log;
73 }
74 consoleLog.clearMock = clearMock;
75})(consoleLog = exports.consoleLog || (exports.consoleLog = {}));
76function getChildValue(object, path, defaultValue) {
77 return path
78 .split(/[\.\[\]\'\"]/)
79 .filter(p => p)
80 .reduce((o, p) => o ? o[p] : defaultValue, object);
81}
82exports.getChildValue = getChildValue;
83function createRoute(...args) {
84 return "/" + args
85 .filter(x => !!x)
86 .map(x => x.toLowerCase())
87 .map(x => x.startsWith("/") ? x.slice(1) : x)
88 .filter(x => !!x)
89 .join("/");
90}
91exports.createRoute = createRoute;
92function mkdirp(path) {
93 var dir = path_1.dirname(path);
94 if (!fs_1.existsSync(dir)) {
95 mkdirp(dir);
96 }
97 fs_1.mkdirSync(path);
98}
99exports.mkdirp = mkdirp;
100//some object can't simply convertible to string https://github.com/emberjs/ember.js/issues/14922#issuecomment-278986178
101function safeToString(value) {
102 try {
103 return value.toString();
104 }
105 catch (e) {
106 return "[object Object]";
107 }
108}
109exports.safeToString = safeToString;