mirror of
https://github.com/feathersjs/feathers.git
synced 2026-01-25 15:23:13 +00:00
150 lines
3.8 KiB
JavaScript
150 lines
3.8 KiB
JavaScript
// Removes all leading and trailing slashes from a path
|
|
exports.stripSlashes = function stripSlashes (name) {
|
|
return name.replace(/^(\/*)|(\/*)$/g, '');
|
|
};
|
|
|
|
// A set of lodash-y utility functions that use ES6
|
|
const _ = exports._ = {
|
|
each (obj, callback) {
|
|
if (obj && typeof obj.forEach === 'function') {
|
|
obj.forEach(callback);
|
|
} else if (_.isObject(obj)) {
|
|
Object.keys(obj).forEach(key => callback(obj[key], key));
|
|
}
|
|
},
|
|
|
|
some (value, callback) {
|
|
return Object.keys(value)
|
|
.map(key => [ value[key], key ])
|
|
.some(([val, key]) => callback(val, key));
|
|
},
|
|
|
|
every (value, callback) {
|
|
return Object.keys(value)
|
|
.map(key => [ value[key], key ])
|
|
.every(([val, key]) => callback(val, key));
|
|
},
|
|
|
|
keys (obj) {
|
|
return Object.keys(obj);
|
|
},
|
|
|
|
values (obj) {
|
|
return _.keys(obj).map(key => obj[key]);
|
|
},
|
|
|
|
isMatch (obj, item) {
|
|
return _.keys(item).every(key => obj[key] === item[key]);
|
|
},
|
|
|
|
isEmpty (obj) {
|
|
return _.keys(obj).length === 0;
|
|
},
|
|
|
|
isObject (item) {
|
|
return (typeof item === 'object' && !Array.isArray(item) && item !== null);
|
|
},
|
|
|
|
extend (...args) {
|
|
return Object.assign(...args);
|
|
},
|
|
|
|
omit (obj, ...keys) {
|
|
const result = _.extend({}, obj);
|
|
keys.forEach(key => delete result[key]);
|
|
return result;
|
|
},
|
|
|
|
pick (source, ...keys) {
|
|
const result = {};
|
|
keys.forEach(key => {
|
|
result[key] = source[key];
|
|
});
|
|
return result;
|
|
},
|
|
|
|
// Recursively merge the source object into the target object
|
|
merge (target, source) {
|
|
if (_.isObject(target) && _.isObject(source)) {
|
|
Object.keys(source).forEach(key => {
|
|
if (_.isObject(source[key])) {
|
|
if (!target[key]) {
|
|
Object.assign(target, { [key]: {} });
|
|
}
|
|
|
|
_.merge(target[key], source[key]);
|
|
} else {
|
|
Object.assign(target, { [key]: source[key] });
|
|
}
|
|
});
|
|
}
|
|
return target;
|
|
}
|
|
};
|
|
|
|
// Return a function that filters a result object or array
|
|
// and picks only the fields passed as `params.query.$select`
|
|
// and additional `otherFields`
|
|
exports.select = function select (params, ...otherFields) {
|
|
const fields = params && params.query && params.query.$select;
|
|
|
|
if (Array.isArray(fields) && otherFields.length) {
|
|
fields.push(...otherFields);
|
|
}
|
|
|
|
const convert = result => {
|
|
if (!Array.isArray(fields)) {
|
|
return result;
|
|
}
|
|
|
|
return _.pick(result, ...fields);
|
|
};
|
|
|
|
return result => {
|
|
if (Array.isArray(result)) {
|
|
return result.map(convert);
|
|
}
|
|
|
|
return convert(result);
|
|
};
|
|
};
|
|
|
|
// An in-memory sorting function according to the
|
|
// $sort special query parameter
|
|
exports.sorter = function sorter ($sort) {
|
|
return function (first, second) {
|
|
let comparator = 0;
|
|
_.each($sort, (modifier, key) => {
|
|
modifier = parseInt(modifier, 10);
|
|
|
|
if (first[key] < second[key]) {
|
|
comparator -= 1 * modifier;
|
|
}
|
|
|
|
if (first[key] > second[key]) {
|
|
comparator += 1 * modifier;
|
|
}
|
|
});
|
|
return comparator;
|
|
};
|
|
};
|
|
|
|
// Duck-checks if an object looks like a promise
|
|
exports.isPromise = function isPromise (result) {
|
|
return _.isObject(result) &&
|
|
typeof result.then === 'function';
|
|
};
|
|
|
|
exports.makeUrl = function makeUrl (path, app = {}) {
|
|
const get = typeof app.get === 'function' ? app.get.bind(app) : () => {};
|
|
const env = get('env') || process.env.NODE_ENV;
|
|
const host = get('host') || process.env.HOST_NAME || 'localhost';
|
|
const protocol = (env === 'development' || env === 'test' || (env === undefined)) ? 'http' : 'https';
|
|
const PORT = get('port') || process.env.PORT || 3030;
|
|
const port = (env === 'development' || env === 'test' || (env === undefined)) ? `:${PORT}` : '';
|
|
|
|
path = path || '';
|
|
|
|
return `${protocol}://${host}${port}/${exports.stripSlashes(path)}`;
|
|
};
|