UNPKG

3.25 kBPlain TextView Raw
1// Removes all leading and trailing slashes from a path
2export function stripSlashes (name: string) {
3 return name.replace(/^(\/+)|(\/+)$/g, '');
4}
5
6export type KeyValueCallback<T> = (value: any, key: string) => T;
7
8// A set of lodash-y utility functions that use ES6
9export const _ = {
10 each (obj: any, callback: KeyValueCallback<void>) {
11 if (obj && typeof obj.forEach === 'function') {
12 obj.forEach(callback);
13 } else if (_.isObject(obj)) {
14 Object.keys(obj).forEach(key => callback(obj[key], key));
15 }
16 },
17
18 some (value: any, callback: KeyValueCallback<boolean>) {
19 return Object.keys(value)
20 .map(key => [ value[key], key ])
21 .some(([val, key]) => callback(val, key));
22 },
23
24 every (value: any, callback: KeyValueCallback<boolean>) {
25 return Object.keys(value)
26 .map(key => [ value[key], key ])
27 .every(([val, key]) => callback(val, key));
28 },
29
30 keys (obj: any) {
31 return Object.keys(obj);
32 },
33
34 values (obj: any) {
35 return _.keys(obj).map(key => obj[key]);
36 },
37
38 isMatch (obj: any, item: any) {
39 return _.keys(item).every(key => obj[key] === item[key]);
40 },
41
42 isEmpty (obj: any) {
43 return _.keys(obj).length === 0;
44 },
45
46 isObject (item: any) {
47 return (typeof item === 'object' && !Array.isArray(item) && item !== null);
48 },
49
50 isObjectOrArray (value: any) {
51 return typeof value === 'object' && value !== null;
52 },
53
54 extend (first: any, ...rest: any[]) {
55 return Object.assign(first, ...rest);
56 },
57
58 omit (obj: any, ...keys: string[]) {
59 const result = _.extend({}, obj);
60 keys.forEach(key => delete result[key]);
61 return result;
62 },
63
64 pick (source: any, ...keys: string[]) {
65 return keys.reduce((result: { [key: string]: any }, key) => {
66 if (source[key] !== undefined) {
67 result[key] = source[key];
68 }
69
70 return result;
71 }, {});
72 },
73
74 // Recursively merge the source object into the target object
75 merge (target: any, source: any) {
76 if (_.isObject(target) && _.isObject(source)) {
77 Object.keys(source).forEach(key => {
78 if (_.isObject(source[key])) {
79 if (!target[key]) {
80 Object.assign(target, { [key]: {} });
81 }
82
83 _.merge(target[key], source[key]);
84 } else {
85 Object.assign(target, { [key]: source[key] });
86 }
87 });
88 }
89 return target;
90 }
91};
92
93// Duck-checks if an object looks like a promise
94export function isPromise (result: any) {
95 return _.isObject(result) &&
96 typeof result.then === 'function';
97}
98
99export function makeUrl (path: string, app: any = {}) {
100 const get = typeof app.get === 'function' ? app.get.bind(app) : () => {};
101 const env = get('env') || process.env.NODE_ENV;
102 const host = get('host') || process.env.HOST_NAME || 'localhost';
103 const protocol = (env === 'development' || env === 'test' || (env === undefined)) ? 'http' : 'https';
104 const PORT = get('port') || process.env.PORT || 3030;
105 const port = (env === 'development' || env === 'test' || (env === undefined)) ? `:${PORT}` : '';
106
107 path = path || '';
108
109 return `${protocol}://${host}${port}/${exports.stripSlashes(path)}`;
110}
111
112export function createSymbol (name: string) {
113 return typeof Symbol !== 'undefined' ? Symbol(name) : name;
114}