@medmain/core
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -0,1 +1,3 @@ | ||
import { cloneDeep, isEmpty } from 'lodash'; | ||
import dotProp from 'dot-prop'; | ||
import cuid from 'cuid'; | ||
@@ -8,6 +10,162 @@ import objectToJSON from 'object-tojson'; | ||
return cuid(); | ||
} // Transforms an array of paths to an object of props | ||
// Example: ['id', 'image.filename'] => {id: true, image: {filename: true}} | ||
export function pathsToProps(pathsOrProps) { | ||
if (Array.isArray(pathsOrProps)) { | ||
var props = {}; | ||
for (var _iterator = pathsOrProps, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
} | ||
var path = _ref; | ||
dotProp.set(props, path, true); | ||
} | ||
return props; | ||
} | ||
if (typeof pathsOrProps === 'object') { | ||
return pathsOrProps; | ||
} | ||
throw new Error(`'pathsOrProps' argument should be an array or an object`); | ||
} // Transforms an object of props to an array of paths | ||
// Example: {id: true, image: {filename: true}} => ['id', 'image.filename'] | ||
export function propsToPaths(propsOrPaths) { | ||
if (Array.isArray(propsOrPaths)) { | ||
return propsOrPaths; | ||
} | ||
if (typeof propsOrPaths === 'object') { | ||
return _getPaths(propsOrPaths); | ||
} | ||
throw new Error(`'propsOrPaths' argument should be an array or an object`); | ||
} | ||
export function getImageFormat(_ref) { | ||
var filename = _ref.filename, | ||
type = _ref.type; | ||
function _getPaths(props) { | ||
var paths = []; | ||
var _arr = Object.entries(props); | ||
for (var _i2 = 0; _i2 < _arr.length; _i2++) { | ||
var _arr$_i = _arr[_i2], | ||
key = _arr$_i[0], | ||
value = _arr$_i[1]; | ||
if (typeof value === 'object' && !(value instanceof Date)) { | ||
var subpaths = _getPaths(value); | ||
for (var _iterator2 = subpaths, _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i3 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i3++]; | ||
} else { | ||
_i3 = _iterator2.next(); | ||
if (_i3.done) break; | ||
_ref2 = _i3.value; | ||
} | ||
var subpath = _ref2; | ||
paths.push(key + '.' + subpath); | ||
} | ||
} else if (value !== undefined) { | ||
paths.push(key); | ||
} | ||
} | ||
return paths; | ||
} | ||
export function pickProps(props, pathsOrProps) { | ||
var paths = propsToPaths(pathsOrProps); | ||
var result = {}; | ||
for (var _iterator3 = paths, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { | ||
var _ref3; | ||
if (_isArray3) { | ||
if (_i4 >= _iterator3.length) break; | ||
_ref3 = _iterator3[_i4++]; | ||
} else { | ||
_i4 = _iterator3.next(); | ||
if (_i4.done) break; | ||
_ref3 = _i4.value; | ||
} | ||
var path = _ref3; | ||
var value = dotProp.get(props, path); | ||
if (value !== undefined) { | ||
dotProp.set(result, path, value); | ||
} | ||
} | ||
return result; | ||
} | ||
export function omitProps(props, pathsOrProps) { | ||
// OPTIMIZE: we shouldn't clone and clean | ||
var paths = propsToPaths(pathsOrProps); | ||
var result = cloneDeep(props); | ||
for (var _iterator4 = paths, _isArray4 = Array.isArray(_iterator4), _i5 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) { | ||
var _ref4; | ||
if (_isArray4) { | ||
if (_i5 >= _iterator4.length) break; | ||
_ref4 = _iterator4[_i5++]; | ||
} else { | ||
_i5 = _iterator4.next(); | ||
if (_i5.done) break; | ||
_ref4 = _i5.value; | ||
} | ||
var path = _ref4; | ||
dotProp.delete(result, path); | ||
} | ||
result = cleanProps(result); | ||
return result; | ||
} | ||
export function cleanProps(props) { | ||
var result = {}; | ||
var _arr2 = Object.entries(props); | ||
for (var _i6 = 0; _i6 < _arr2.length; _i6++) { | ||
var _arr2$_i = _arr2[_i6], | ||
key = _arr2$_i[0], | ||
value = _arr2$_i[1]; | ||
if (typeof value === 'object') { | ||
value = cleanProps(value); | ||
if (isEmpty(value)) { | ||
value = undefined; | ||
} | ||
} | ||
if (value !== undefined) { | ||
result[key] = value; | ||
} | ||
} | ||
return result; | ||
} | ||
export function getImageFormat(_ref5) { | ||
var filename = _ref5.filename, | ||
type = _ref5.type; | ||
type = (type || '').toLowerCase(); | ||
@@ -14,0 +172,0 @@ |
@@ -6,5 +6,14 @@ "use strict"; | ||
exports.generateId = generateId; | ||
exports.pathsToProps = pathsToProps; | ||
exports.propsToPaths = propsToPaths; | ||
exports.pickProps = pickProps; | ||
exports.omitProps = omitProps; | ||
exports.cleanProps = cleanProps; | ||
exports.getImageFormat = getImageFormat; | ||
exports.imageFormatToType = imageFormatToType; | ||
var _lodash = require("lodash"); | ||
var _dotProp = _interopRequireDefault(require("dot-prop")); | ||
var _cuid = _interopRequireDefault(require("cuid")); | ||
@@ -22,4 +31,104 @@ | ||
return (0, _cuid.default)(); | ||
} // Transforms an array of paths to an object of props | ||
// Example: ['id', 'image.filename'] => {id: true, image: {filename: true}} | ||
function pathsToProps(pathsOrProps) { | ||
if (Array.isArray(pathsOrProps)) { | ||
const props = {}; | ||
for (const path of pathsOrProps) { | ||
_dotProp.default.set(props, path, true); | ||
} | ||
return props; | ||
} | ||
if (typeof pathsOrProps === 'object') { | ||
return pathsOrProps; | ||
} | ||
throw new Error(`'pathsOrProps' argument should be an array or an object`); | ||
} // Transforms an object of props to an array of paths | ||
// Example: {id: true, image: {filename: true}} => ['id', 'image.filename'] | ||
function propsToPaths(propsOrPaths) { | ||
if (Array.isArray(propsOrPaths)) { | ||
return propsOrPaths; | ||
} | ||
if (typeof propsOrPaths === 'object') { | ||
return _getPaths(propsOrPaths); | ||
} | ||
throw new Error(`'propsOrPaths' argument should be an array or an object`); | ||
} | ||
function _getPaths(props) { | ||
const paths = []; | ||
for (const [key, value] of Object.entries(props)) { | ||
if (typeof value === 'object' && !(value instanceof Date)) { | ||
const subpaths = _getPaths(value); | ||
for (const subpath of subpaths) { | ||
paths.push(key + '.' + subpath); | ||
} | ||
} else if (value !== undefined) { | ||
paths.push(key); | ||
} | ||
} | ||
return paths; | ||
} | ||
function pickProps(props, pathsOrProps) { | ||
const paths = propsToPaths(pathsOrProps); | ||
const result = {}; | ||
for (const path of paths) { | ||
const value = _dotProp.default.get(props, path); | ||
if (value !== undefined) { | ||
_dotProp.default.set(result, path, value); | ||
} | ||
} | ||
return result; | ||
} | ||
function omitProps(props, pathsOrProps) { | ||
// OPTIMIZE: we shouldn't clone and clean | ||
const paths = propsToPaths(pathsOrProps); | ||
let result = (0, _lodash.cloneDeep)(props); | ||
for (const path of paths) { | ||
_dotProp.default.delete(result, path); | ||
} | ||
result = cleanProps(result); | ||
return result; | ||
} | ||
function cleanProps(props) { | ||
const result = {}; | ||
for (let [key, value] of Object.entries(props)) { | ||
if (typeof value === 'object') { | ||
value = cleanProps(value); | ||
if ((0, _lodash.isEmpty)(value)) { | ||
value = undefined; | ||
} | ||
} | ||
if (value !== undefined) { | ||
result[key] = value; | ||
} | ||
} | ||
return result; | ||
} | ||
function getImageFormat({ | ||
@@ -26,0 +135,0 @@ filename, |
@@ -0,1 +1,3 @@ | ||
import { cloneDeep, isEmpty } from 'lodash'; | ||
import dotProp from 'dot-prop'; | ||
import cuid from 'cuid'; | ||
@@ -8,3 +10,99 @@ import objectToJSON from 'object-tojson'; | ||
return cuid(); | ||
} // Transforms an array of paths to an object of props | ||
// Example: ['id', 'image.filename'] => {id: true, image: {filename: true}} | ||
export function pathsToProps(pathsOrProps) { | ||
if (Array.isArray(pathsOrProps)) { | ||
const props = {}; | ||
for (const path of pathsOrProps) { | ||
dotProp.set(props, path, true); | ||
} | ||
return props; | ||
} | ||
if (typeof pathsOrProps === 'object') { | ||
return pathsOrProps; | ||
} | ||
throw new Error(`'pathsOrProps' argument should be an array or an object`); | ||
} // Transforms an object of props to an array of paths | ||
// Example: {id: true, image: {filename: true}} => ['id', 'image.filename'] | ||
export function propsToPaths(propsOrPaths) { | ||
if (Array.isArray(propsOrPaths)) { | ||
return propsOrPaths; | ||
} | ||
if (typeof propsOrPaths === 'object') { | ||
return _getPaths(propsOrPaths); | ||
} | ||
throw new Error(`'propsOrPaths' argument should be an array or an object`); | ||
} | ||
function _getPaths(props) { | ||
const paths = []; | ||
for (const [key, value] of Object.entries(props)) { | ||
if (typeof value === 'object' && !(value instanceof Date)) { | ||
const subpaths = _getPaths(value); | ||
for (const subpath of subpaths) { | ||
paths.push(key + '.' + subpath); | ||
} | ||
} else if (value !== undefined) { | ||
paths.push(key); | ||
} | ||
} | ||
return paths; | ||
} | ||
export function pickProps(props, pathsOrProps) { | ||
const paths = propsToPaths(pathsOrProps); | ||
const result = {}; | ||
for (const path of paths) { | ||
const value = dotProp.get(props, path); | ||
if (value !== undefined) { | ||
dotProp.set(result, path, value); | ||
} | ||
} | ||
return result; | ||
} | ||
export function omitProps(props, pathsOrProps) { | ||
// OPTIMIZE: we shouldn't clone and clean | ||
const paths = propsToPaths(pathsOrProps); | ||
let result = cloneDeep(props); | ||
for (const path of paths) { | ||
dotProp.delete(result, path); | ||
} | ||
result = cleanProps(result); | ||
return result; | ||
} | ||
export function cleanProps(props) { | ||
const result = {}; | ||
for (let [key, value] of Object.entries(props)) { | ||
if (typeof value === 'object') { | ||
value = cleanProps(value); | ||
if (isEmpty(value)) { | ||
value = undefined; | ||
} | ||
} | ||
if (value !== undefined) { | ||
result[key] = value; | ||
} | ||
} | ||
return result; | ||
} | ||
export function getImageFormat({ | ||
@@ -11,0 +109,0 @@ filename, |
{ | ||
"name": "@medmain/core", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Medmain's core module, root of everything else", | ||
@@ -13,2 +13,3 @@ "files": [ | ||
"cuid": "^2.1.1", | ||
"dot-prop": "^4.2.0", | ||
"object-tojson": "^1.0.1" | ||
@@ -15,0 +16,0 @@ }, |
19041
612
3
+ Addeddot-prop@^4.2.0
+ Addeddot-prop@4.2.1(transitive)
+ Addedis-obj@1.0.1(transitive)