1.0.1 • Published 5 years ago

gulp-replace-each v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

gulp-replace-each Build Status

Install

$ npm install --save gulp-replace-each

Usage

testFile.js

class Test {
    constructor() {
        this.name = 'Test';
    }

    toString() {
        console.log(this.name)
    }

    saySomething() {
        this.toString();
    }

    sayTestManyTimes(num) {
        if (!num || num < 1) {
            num = 1;
        }
        for (let i = 0; i < num; i++) {
            this.toString();
        }
    }
}

gulpfile.js

const gulp = require('gulp');
const replaceEach = require('gulp-replace-each');

const KEYWORDS = [
    '(Test)',
    '(saySomething)',
].map((value) => { return new RegExp(value, 'g'); });

gulp.task('replace-all', () => {
    return gulp.task(['testFile.js'])
        .pipe(replaceEach(
            KEYWORDS /* array or just RegExp object */,
            (arrayIndex, value, _1) => { // the first argument is array index, other arguments are arguments from string replace function callback
            let result = _1;

            if (arrayIndex === 1) {
                result += 'Funny';
            } else {
                result += '1';
            }

            return result;
        }))
        .pipe(gulp.dest('dist'))
});

dist/testFile.js

class Test1 {
    constructor() {
        this.name = 'Test1';
    }

    toString() {
        console.log(this.name)
    }

    saySomethingFunny() {
        this.toString();
    }

    sayTest1ManyTimes(num) {
        if (!num || num < 1) {
            num = 1;
        }
        for (let i = 0; i < num; i++) {
            this.toString();
        }
    }
}