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    
view-container / dist / no-parser / flatten.js
Size: Mime:
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
// @flow
const exotic_1 = require("exotic");
const flatten_1 = __importStar(require("../utils/flatten"));
const isRuleSet = (interpolation) => !!(interpolation &&
    Array.isArray(interpolation) &&
    interpolation.length > 0 &&
    interpolation[0] &&
    Array.isArray(interpolation[0]));
const flatten = (chunks, executionContext) => {
    /* Fall back to old flattener for non-rule-set chunks */
    if (!isRuleSet(chunks)) {
        return flatten_1.default(chunks, executionContext);
    }
    return chunks.reduce((ruleSet, chunk) => {
        if (!Array.isArray(chunk)) {
            return ruleSet;
        }
        let appendChunks = [];
        const newChunk = chunk.reduce((rules, rule) => {
            /* Remove falsey values */
            if (rule === undefined ||
                rule === null ||
                rule === false ||
                rule === '') {
                return rules;
            }
            /* Flatten nested rule set */
            if (isRuleSet(rule)) {
                // $FlowFixMe Don't know what's wrong here
                appendChunks = [...appendChunks, ...flatten(rule, executionContext)];
                return rules;
            }
            /* Stringify unexpected array */
            if (Array.isArray(rule)) {
                return [...rules, ...flatten_1.default(rule, executionContext)];
            }
            /* Either execute or defer the function */
            if (typeof rule === 'function') {
                if (executionContext) {
                    const res = rule(executionContext);
                    if (isRuleSet(res)) {
                        appendChunks = [
                            ...appendChunks,
                            // $FlowFixMe Don't know what's wrong here
                            ...flatten(res, executionContext),
                        ];
                        return rules;
                    }
                    /* Flatten non-ruleset values */
                    return [...rules, ...flatten([res], executionContext)];
                }
                else {
                    return [...rules, rule];
                }
            }
            /* Handle other components */
            if (typeof rule === 'object' &&
                rule.hasOwnProperty('styledComponentId')) {
                return [...rules, `.${rule.styledComponentId}`];
            }
            /* Convert object to css string */
            if (typeof rule === 'object' && exotic_1.isPlainObject(rule)) {
                return [...rules, flatten_1.objToCss(rule)];
            }
            return [...rules, rule.toString()];
        }, []);
        if (executionContext) {
            const newChunkStr = newChunk.join('');
            if (appendChunks.length) {
                return [...ruleSet, newChunkStr, ...appendChunks];
            }
            return [...ruleSet, newChunkStr];
        }
        return [...ruleSet, newChunk, ...appendChunks];
    }, []);
};
exports.default = flatten;
//# sourceMappingURL=flatten.js.map