/usr/lib/nodejs/applause/src/applause.js is in node-applause 1.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | /*
* applause
*
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/applause/blob/master/LICENSE-MIT
*/
// dependencies
var _ = require('lodash');
var plugins = require('./plugins');
// took from MDN
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
var escapeRegExp = function (string) {
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
};
var getPatterns = function (applause) {
var opts = applause.options;
// shallow patterns
var patterns = _.chain(opts.patterns)
.clone()
.compact()
.filter(function (pattern) {
return !_.isEmpty(pattern);
})
.value();
// backward compatibility
var variables = opts.variables;
if (!_.isEmpty(variables)) {
patterns.push({
json: variables
});
}
// process
for (var i = patterns.length - 1; i >= 0; i -= 1) {
var pattern = patterns[i];
// plugins
plugins.forEach(function (plugin) {
if (plugin.match(pattern, opts) === true) {
plugin.transform(pattern, opts, function (items) {
if (items instanceof Error) {
throw items;
} else {
// store transformed pattern in context
pattern = items;
}
});
} else {
// plugin doesn't apply
}
});
// convert to array
if (!_.isArray(pattern)) {
pattern = [pattern];
}
// link with pattern with original source
pattern.forEach(function (pattern) {
pattern.source = patterns[i];
});
// attach index
Array.prototype.splice.apply(patterns, [i, 1].concat(pattern));
}
if (opts.preserveOrder !== true) {
// only sort non regex patterns (prevents replace issues like head, header)
patterns.sort(function (a, b) {
var x = a.match;
var y = b.match;
if (_.isString(x) && _.isString(y)) {
return y.length - x.length;
} else if (_.isString(x)) {
return -1;
}
return 1;
});
}
return patterns;
};
// applause
var Applause = function (opts) {
this.options = _.defaults(opts, {
patterns: [],
prefix: opts.usePrefix === false ? '' : '@@',
usePrefix: true,
preservePrefix: false,
delimiter: '.',
preserveOrder: false
});
};
Applause.prototype.replace = function (content, process) {
var opts = this.options;
// prevent null
content = content || '';
// prepare patterns
var patterns = getPatterns(this);
var detail = [];
var total_count = 0;
// iterate over each pattern and make replacement
patterns.forEach(function (pattern, i) {
// filter empty patterns
var match = pattern.match;
// support replace flag too
var replacement = pattern.replacement;
if (replacement === undefined || replacement === null) {
replacement = pattern.replace;
}
var source = pattern.source;
var expression = false;
// match check
if (match !== undefined && match !== null) {
if (_.isRegExp(match)) {
expression = true;
} else if (_.isString(match)) {
if (match.length > 0) {
match = new RegExp(opts.prefix + escapeRegExp(match), 'g');
} else {
// empty match
return;
}
} else {
throw new Error('Unsupported match type (RegExp or String expected).');
}
} else {
throw new Error('Match attribute expected in pattern definition.');
}
// replacement check
if (replacement !== undefined && replacement !== null) {
if (!_.isFunction(replacement)) {
if (!_.isString(replacement)) {
// transform object to string
replacement = JSON.stringify(replacement);
}
if (expression === false) {
// escape dollar sequences in easy mode
replacement = replacement.replace(/\$/g, '$$$');
// preserve prefix
if (opts.preservePrefix === true) {
replacement = opts.prefix + replacement;
}
}
} else {
// replace using function return value
replacement = function () {
var args = Array.prototype.slice.call(arguments);
return pattern.replacement.apply(this, args.concat(process || []));
};
}
} else {
throw new Error('Replacement attribute expected in pattern definition.');
}
// replace logic
var count = (content.match(match) || []).length;
if (count > 0) {
// update content
content = content.replace(match, replacement);
// save detail data
detail.push({
match: match,
replacement: replacement,
source: pattern.source,
count: count
});
total_count += count;
}
});
// FIXME: always return detailed result
if (detail.length === 0) {
content = false;
}
return {
content: content,
detail: detail,
count: total_count
};
};
// static
Applause.create = function (opts) {
return new Applause(opts);
};
Applause.version = require('../package.json').version;
// expose
module.exports = Applause;
|