Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
exotic-core / dist / src / coerce / toShape.js
Size: Mime:
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const deps_1 = require("../../deps");
const types_1 = require("../../types");
const Typed_1 = __importDefault(require("../Typed"));
/**
 * @NOTE this is `filterObj`
 * @desc dereferences obj, deletes property
 * @param  {string} path
 * @param  {Object} obj
 * @return {Object}
 */
const withoutProp = (path, obj) => {
    const predicate = (value, key) => key !== path;
    return deps_1.filterWhere(obj, predicate);
};
exports.withoutProp = withoutProp;
/**
 * @TODO we may not use flat paths the whole way through, but it reduces recursion
 *
 * @param {Object} data convert primitives to their correct type
 * @return {Object} autofixed data
 */
const toFlatAutoCoercedPrimitives = data => {
    const primitives = [Typed_1.default(Boolean), Typed_1.default(Number), Typed_1.default(String)];
    const find = (value, path) => {
        return primitives.find(Type => Type.is(value, path)) || { coerce: deps_1.identity };
    };
    const flat = deps_1.toUniverseView(data, true);
    // @TODO for history, ON THE ACTUAL OBJECT
    // defineFinal(flat, 'original', original)
    deps_1.forOwn(flat, (value, path) => {
        // which primitive wants this one
        const Type = find(value, path);
        // upgrade it
        flat[path] = Type.coerce(value, path);
    });
    return flat;
};
const fromFlatToNested = flat => {
    const target = {};
    deps_1.forOwn(flat, (value, path) => {
        deps_1.set(target, path, value);
    });
    return target;
};
const thereAndFlatAgain = data => {
    console.log('thereAndFlatAgain');
    const defaulted = types_1.toEmpty(data);
    const flattened = toFlatAutoCoercedPrimitives(data);
    const nested = fromFlatToNested(flattened);
    return nested;
};
exports.thereAndFlatAgain = thereAndFlatAgain;
/**
 * @NOTE @TODO handle mapping of paths IN SCHEMA OR MAP
 *
 * @param {Object} schema type definitions for data
 * @param {Object<string, string>} pathMap paths to remap & lift
 * @return {Object} lifted & merged
 *
 * @see lift
 */
function mapDataWith(schema, pathMap, data) {
    const target = {};
    const simple = lift(data, 'children');
    // value, key, obj
    deps_1.forOwn(pathMap, (sourcePath, destinationPath) => {
        // when we have this path in our data
        if (deps_1.has(simple, sourcePath)) {
            // then set it on our target
            const value = deps_1.get(simple, sourcePath);
            deps_1.set(target, destinationPath, value);
        }
        else {
            // @TODO otherwise, default, or...
        }
    });
}
exports.mapDataWith = mapDataWith;
/**
 * @param {Object} data object to lift on
 * @param {String|Array<string>} property paths
 * @return {Object} lifted & merged
 */
function lift(data, property) {
    const value = deps_1.get(data, property);
    if (!value) {
        return data;
    }
    const merged = deps_1.merge(data, value, { clone: true });
    const selected = withoutProp(property, merged);
    return selected;
}
exports.lift = lift;
const liftCurried = deps_1.curry2(lift);
const mapDataWithCurried = deps_1.curry3(mapDataWith);
const withoutPropCurried = deps_1.curry2(withoutProp);
//# sourceMappingURL=toShape.js.map