Sha256: 308e02c276fea613a258069fef002d7789f965877e19332d44f978dc266178e4

Contents?: true

Size: 1.17 KB

Versions: 11

Compression:

Stored size: 1.17 KB

Contents

// alter.js
// MIT licensed, see LICENSE file
// Copyright (c) 2013 Olov Lassus <olov.lassus@gmail.com>

var assert = require("assert");
var stableSort = require("stable");

// fragments is a list of {start: index, end: index, str: string to replace with}
function alter(str, fragments) {
    "use strict";

    var isArray = Array.isArray || function(v) {
        return Object.prototype.toString.call(v) === "[object Array]";
    };;

    assert(typeof str === "string");
    assert(isArray(fragments));

    // stableSort isn't in-place so no need to copy array first
    var sortedFragments = stableSort(fragments, function(a, b) {
        return a.start - b.start;
    });

    var outs = [];

    var pos = 0;
    for (var i = 0; i < sortedFragments.length; i++) {
        var frag = sortedFragments[i];

        assert(pos <= frag.start);
        assert(frag.start <= frag.end);
        outs.push(str.slice(pos, frag.start));
        outs.push(frag.str);
        pos = frag.end;
    }
    if (pos < str.length) {
        outs.push(str.slice(pos));
    }

    return outs.join("");
}

if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
    module.exports = alter;
}

Version data entries

11 entries across 11 versions & 4 rubygems

Version Path
ilog-0.4.1 node_modules/alter/alter.js
ilog-0.4.0 node_modules/alter/alter.js
ilog-0.3.3 node_modules/alter/alter.js
xcodebuild-helper-1.2.5 externals/ios-sim-master/node_modules/alter/alter.js
xcodebuild-helper-1.2.3 externals/ios-sim-master/node_modules/alter/alter.js
es6_tilt-0.1.2 test/dummy/app/assets/javascripts/node_modules/alter/alter.js
es6_tilt-0.1.1 test/dummy/app/assets/javascripts/node_modules/alter/alter.js
es6_tilt-0.1.0 test/dummy/app/assets/javascripts/node_modules/alter/alter.js
gulp_assets-1.0.0.pre.5 template/node_modules/babel-core/node_modules/regenerator/node_modules/defs/node_modules/alter/alter.js
gulp_assets-1.0.0.pre.4 template/node_modules/babel-core/node_modules/regenerator/node_modules/defs/node_modules/alter/alter.js
gulp_assets-1.0.0.pre.3 template/node_modules/babel-core/node_modules/regenerator/node_modules/defs/node_modules/alter/alter.js