pax_global_header 0000666 0000000 0000000 00000000064 12577711720 0014523 g ustar 00root root 0000000 0000000 52 comment=0552ea5606defc6e4d808d5a57a38044b5a9d1e8
applause-1.2.2/ 0000775 0000000 0000000 00000000000 12577711720 0013337 5 ustar 00root root 0000000 0000000 applause-1.2.2/.editorconfig 0000664 0000000 0000000 00000000254 12577711720 0016015 0 ustar 00root root 0000000 0000000 root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
applause-1.2.2/.gitignore 0000664 0000000 0000000 00000000141 12577711720 0015323 0 ustar 00root root 0000000 0000000 # generics
node_modules
npm-debug.log
tmp
.DS_Store
# meteor files
/.npm
/.versions
/package.js
applause-1.2.2/.jshintrc 0000664 0000000 0000000 00000000321 12577711720 0015160 0 ustar 00root root 0000000 0000000 {
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"node": true,
"sub": true,
"undef": true,
"unused": true
}
applause-1.2.2/.travis.yml 0000664 0000000 0000000 00000000164 12577711720 0015451 0 ustar 00root root 0000000 0000000 sudo: false
language: node_js
node_js:
- 'iojs'
- '0.12'
- '0.10'
before_script:
- npm install -g grunt-cli
applause-1.2.2/Gruntfile.js 0000664 0000000 0000000 00000004676 12577711720 0015651 0 ustar 00root root 0000000 0000000
/*
* applause
*
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/applause/blob/master/LICENSE-MIT
*/
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
src: {
src: ['src/**/*.js']
},
test: {
src: ['test/**/*.js']
}
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: '<%= jshint.test.src %>'
}
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
src: {
files: '<%= jshint.src.src %>',
tasks: ['jshint:src', 'mochaTest']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'mochaTest']
}
},
exec: {
'meteor-init': {
command: [
// Make sure Meteor is installed, per https://meteor.com/install.
// The curl'ed script is safe; takes 2 minutes to read source & check.
'type meteor > /dev/null 2>&1 || { curl https://install.meteor.com/ | sh; }',
// Meteor expects package.js to be in the root directory of
// the checkout, so copy it there temporarily
'cp meteor/package.js .'
].join(';')
},
// !- only add this if there was no "clean" task
'meteor-cleanup': {
// remove build files and package.js
command: 'rm -rf .build.* versions.json package.js'
},
'meteor-test': {
command: 'node_modules/.bin/spacejam --mongo-url mongodb:// test-packages ./'
},
'meteor-publish': {
command: 'meteor publish'
}
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-exec');
grunt.registerTask('test', ['mochaTest']);
grunt.registerTask('default', ['jshint', 'test']);
// https://github.com/MeteorPackaging/grunt-gulp-meteor
grunt.registerTask('meteor-test', ['exec:meteor-init', 'exec:meteor-test', 'exec:meteor-cleanup']);
grunt.registerTask('meteor-publish', ['exec:meteor-init', 'exec:meteor-publish', 'exec:meteor-cleanup']);
grunt.registerTask('meteor', ['exec:meteor-init', 'exec:meteor-test', 'exec:meteor-publish', 'exec:meteor-cleanup']);
};
applause-1.2.2/LICENSE-MIT 0000664 0000000 0000000 00000002034 12577711720 0014772 0 ustar 00root root 0000000 0000000 Copyright (c) 2015 outaTiME
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
applause-1.2.2/README.md 0000664 0000000 0000000 00000027244 12577711720 0014627 0 ustar 00root root 0000000 0000000 # Applause [](https://travis-ci.org/outaTiME/applause) [](https://npmjs.org/package/applause)
Pattern replacer that helps creating human-friendly replacements.
## Install
First make sure you have installed the latest version of [node.js](http://nodejs.org/)
(You may need to restart your computer after this step).
From NPM for use as a command line app:
```shell
npm install applause -g
```
From NPM for programmatic use:
```shell
npm install applause
```
From Git:
```shell
git clone git://github.com/outaTiME/applause
cd applause
npm link .
```
## API Reference
Assuming installation via NPM, you can use `applause` in your application like this:
```javascript
var fs = require('fs');
var Applause = require('applause');
var options = {
patterns: [
{
match: 'foo',
replacement: 'bar'
}
]
};
var applause = Applause.create(options);
var contents = '@@foo';
var result = applause.replace(contents);
console.log(result.content); // bar
```
### Applause Options
#### patterns
Type: `Array`
Define patterns that will be used to replace the contents of source files.
#### patterns.match
Type: `String|RegExp`
Indicates the matching expression.
If matching type is `String` we use a simple variable lookup mechanism `@@string` (in any other case we use the default regexp replace logic):
```javascript
{
patterns: [
{
match: 'foo',
replacement: 'bar' // replaces "@@foo" to "bar"
}
]
}
```
#### patterns.replacement or patterns.replace
Type: `String|Function|Object`
Indicates the replacement for match, for more information about replacement check out the [String.replace].
You can specify a function as replacement. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string.
```javascript
{
patterns: [
{
match: /foo/g,
replacement: function () {
return 'bar'; // replaces "foo" to "bar"
}
}
]
}
```
Also supports object as replacement (we create string representation of object using [JSON.stringify]):
```javascript
{
patterns: [
{
match: /foo/g,
replacement: [1, 2, 3] // replaces "foo" with string representation of "array" object
}
]
}
```
> The replacement only resolve the [special replacement patterns] when using regexp for matching.
[String.replace]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
[JSON.stringify]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[special replacement patterns]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
#### patterns.json
Type: `Object`
If an attribute `json` is found in pattern definition we flatten the object using `delimiter` concatenation and each key–value pair will be used for the replacement (simple variable lookup mechanism and no regexp support).
```javascript
{
patterns: [
{
json: {
"key": "value" // replaces "@@key" to "value"
}
}
]
}
```
Also supports nested objects:
```javascript
{
patterns: [
{
json: {
"key": "value", // replaces "@@key" to "value"
"inner": { // replaces "@@inner" with string representation of "inner" object
"key": "value" // replaces "@@inner.key" to "value"
}
}
}
]
}
```
For deferred invocations is possible to define functions:
```javascript
{
patterns: [
{
json: function (done) {
done({
key: 'value'
});
}
}
]
}
```
#### patterns.yaml
Type: `String`
If an attribute `yaml` found in pattern definition it will be converted and then processed like [json attribute](#patternsjson).
```javascript
{
patterns: [
{
yaml: 'key: value' // replaces "@@key" to "value"
}
]
}
```
For deferred invocations is possible to define functions:
```javascript
{
patterns: [
{
yaml: function (done) {
done('key: value');
}
}
]
}
```
#### patterns.cson
Type: `String`
If an attribute `cson` is found in pattern definition it will be converted and then processed like [json attribute](#patternsjson).
```javascript
{
patterns: [
{
cson: 'key: \'value\''
}
]
}
```
For deferred invocations is possible to define functions:
```javascript
{
patterns: [
{
cson: function (done) {
done('key: \'value\'');
}
}
]
}
```
#### variables
Type: `Object`
This is the old way to define patterns using plain object (simple variable lookup mechanism and no regexp support). You can still use this but for more control you should use the new `patterns` way.
```javascript
{
variables: {
'key': 'value' // replaces "@@key" to "value"
}
}
```
#### prefix
Type: `String`
Default: `@@`
The prefix added for matching (prevent bad replacements / easy way).
> This only applies for simple variable lookup mechanism.
#### usePrefix
Type: `Boolean`
Default: `true`
If set to `false`, we match the pattern without `prefix` concatenation (useful when you want to lookup a simple string).
> This only applies for simple variable lookup mechanism.
#### preservePrefix
Type: `Boolean`
Default: `false`
If set to `true`, we preserve the `prefix` in target.
> This only applies for simple variable lookup mechanism and when `patterns.replacement` is a string.
#### delimiter
Type: `String`
Default: `.`
The delimiter used to flatten when using object as replacement.
#### preserveOrder
Type: `Boolean`
Default: `false`
If set to `true`, we preserve the patterns definition order, otherwise these will be sorted (in ascending order) to prevent replacement issues like `head` / `header` (typo regexps will be resolved at last).
### Usage Examples
#### Basic
File `src/manifest.appcache`:
```
CACHE MANIFEST
# @@timestamp
CACHE:
favicon.ico
index.html
NETWORK:
*
```
Node:
```js
var fs = require('fs');
var Applause = require('applause');
var options = {
patterns: [
{
match: 'timestamp',
replacement: new Date().getTime()
}
]
};
var applause = Applause.create(options);
var contents = fs.readFileSync('./src/manifest.appcache', 'utf8');
var result = applause.replace(contents);
console.log(result.content); // replaced output
```
#### Multiple matching
File `src/manifest.appcache`:
```
CACHE MANIFEST
# @@timestamp
CACHE:
favicon.ico
index.html
NETWORK:
*
```
File `src/humans.txt`:
```
__ _
_ _/__ /./|,//_`
/_//_// /_|/// //_, outaTiME v.@@version
/* TEAM */
Web Developer / Graphic Designer: Ariel Oscar Falduto
Site: http://www.outa.im
Twitter: @outa7iME
Contact: afalduto at gmail dot com
From: Buenos Aires, Argentina
/* SITE */
Last update: @@timestamp
Standards: HTML5, CSS3, robotstxt.org, humanstxt.org
Components: H5BP, Modernizr, jQuery, Twitter Bootstrap, LESS, Jade, Grunt
Software: Sublime Text 2, Photoshop, LiveReload
```
Node:
```js
var fs = require('fs');
var pkg = require('./package.json');
var Applause = require('applause');
var options = {
patterns: [
{
match: 'version',
replacement: pkg.version
},
{
match: 'timestamp',
replacement: new Date().getTime()
}
]
};
var applause = Applause.create(options);
var contents = fs.readFileSync('./src/manifest.appcache', 'utf8');
var result = applause.replace(contents);
console.log(result.content); // replaced output
contents = fs.readFileSync('./src/humans.txt', 'utf8');
result = applause.replace(contents);
console.log(result.content); // replaced output
```
#### Cache busting
File `src/index.html`:
```html
```
Node:
```js
var fs = require('fs');
var Applause = require('applause');
var options = {
patterns: [
{
match: 'timestamp',
replacement: new Date().getTime()
}
]
};
var applause = Applause.create(options);
var contents = fs.readFileSync('./src/index.html', 'utf8');
var result = applause.replace(contents);
console.log(result.content); // replaced output
```
#### Include file
File `src/index.html`:
```html
@@include
```
Node:
```js
var fs = require('fs');
var Applause = require('applause');
var options = {
patterns: [
{
match: 'include',
replacement: fs.readFileSync('./includes/content.html', 'utf8')
}
]
};
var applause = Applause.create(options);
var contents = fs.readFileSync('./src/index.html', 'utf8');
var result = applause.replace(contents);
console.log(result.content); // replaced output
```
#### Regular expression
File `src/username.txt`:
```
John Smith
```
Node:
```js
var fs = require('fs');
var Applause = require('applause');
var options = {
patterns: [
{
match: /(\w+)\s(\w+)/,
replacement: '$2, $1' // replaces "John Smith" to "Smith, John"
}
]
};
var applause = Applause.create(options);
var contents = fs.readFileSync('./username.txt', 'utf8');
var result = applause.replace(contents);
console.log(result.content); // replaced output
```
#### Lookup for `foo` instead of `@@foo`
Node:
```js
var Applause = require('applause');
// option 1 (explicitly using an regexp)
var applause_op1 = Applause.create({
patterns: [
{
match: /foo/g,
replacement: 'bar'
}
]
});
// option 2 (easy way)
var applause_op2 = Applause.create({
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
usePrefix: false
});
// option 3 (old way)
var applause_op3 = Applause.create({
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
prefix: '' // remove prefix
});
```
## Release History
* 2015-09-21 v1.2.2 Update VERSION to lower (version).
* 2015-09-21 v1.2.1 Better support for browserify.
* 2015-09-15 v1.2.0 Use detailed response for replaces with content, detail and count.
* 2015-09-08 v1.1.0 Readme updated. Improvements in handling patterns. Fix plain object representation issue. More test cases.
* 2015-08-11 v1.0.0 Version stabilization, Meteor integration and package.json update.
* 2015-08-06 v0.4.3 Fix issue with special characters attributes ($$, $&, $`, $', $n or $nn) on JSON, YAML and CSON.
* 2015-05-07 v0.4.1 Fix regression issue with empty string in replacement.
* 2015-05-01 v0.4.0 New test cases, parse CSON using [@groupon](https://github.com/groupon/cson-parser), replace alias now supported, new detail flag and third party dependencies updated.
* 2014-10-10 v0.3.4 Escape regexp when matching type is `String`.
* 2014-06-10 v0.3.3 Remove node v.8.0 support and third party dependencies updated.
* 2014-04-18 v0.3.2 JSON / YAML / CSON as function supported. Readme updated (thanks [@milanlandaverde](https://github.com/milanlandaverde)).
* 2014-03-23 v0.3.1 Readme updated.
* 2014-03-22 v0.3.0 Performance improvements. Expression flag removed. New pattern matching for CSON object. More test cases, readme updated and code cleanup.
* 2014-03-21 v0.2.0 Project rename from `pattern-replace` to `applause` (thanks Lady Gaga). Test cases in Mocha and readme updated.
* 2014-03-11 v0.1.2 New pattern matching for YAML object. New preserveOrder flag.
* 2014-02-26 v0.1.1 Remove the force flag (only applies in grunt plugin).
* 2014-02-25 v0.1.0 Initial version.
---
Task submitted by [Ariel Falduto](http://outa.im/)
applause-1.2.2/bin/ 0000775 0000000 0000000 00000000000 12577711720 0014107 5 ustar 00root root 0000000 0000000 applause-1.2.2/bin/replace 0000775 0000000 0000000 00000000447 12577711720 0015455 0 ustar 00root root 0000000 0000000 #!/usr/bin/env node
var argv = require('optimist')
.usage('Usage: $0 [options] ')
/* .check(function (argv) {
if (!argv._.length) {
throw 'Must define at least one file or directory.';
}
}) */
.argv;
var Replacer = require('../lib/replacer');
applause-1.2.2/meteor/ 0000775 0000000 0000000 00000000000 12577711720 0014632 5 ustar 00root root 0000000 0000000 applause-1.2.2/meteor/export.js 0000664 0000000 0000000 00000000045 12577711720 0016510 0 ustar 00root root 0000000 0000000
Applause = Npm.require('applause');
applause-1.2.2/meteor/package.js 0000664 0000000 0000000 00000001431 12577711720 0016562 0 ustar 00root root 0000000 0000000 // package metadata file for Meteor.js
'use strict';
var packageName = 'outatime:applause';
var where = 'server'; // where to install: 'client' or 'server'. For both, pass nothing.
var packageJson = JSON.parse(Npm.require("fs").readFileSync('package.json'));
Package.describe({
name: packageName,
summary: 'Applause (official): ' + packageJson.description,
version: packageJson.version,
git: 'https://github.com/outaTiME/applause.git'
});
Package.onUse(function (api) {
api.versionsFrom(['METEOR@0.9.0', 'METEOR@1.0']);
api.export('Applause', where);
api.addFiles('meteor/export.js', where);
});
Package.onTest(function (api) {
api.use(packageName);
api.use('tinytest');
api.addFiles('meteor/test.js', where);
});
Npm.depends({
"applause": packageJson.version
});
applause-1.2.2/meteor/test.js 0000664 0000000 0000000 00000000135 12577711720 0016146 0 ustar 00root root 0000000 0000000 'use strict';
Tinytest.add('applause.is', function (test) {
test.isNotNull(Applause);
});
applause-1.2.2/package.json 0000664 0000000 0000000 00000001705 12577711720 0015630 0 ustar 00root root 0000000 0000000 {
"name": "applause",
"description": "Pattern replacer that helps creating human-friendly replacements.",
"version": "1.2.2",
"author": {
"name": "outaTiME",
"url": "http://outa.im/"
},
"repository": "outaTiME/applause",
"license": "MIT",
"engines": {
"node": ">= 0.10.0"
},
"scripts": {
"release": "scripts/release.sh",
"test": "grunt test"
},
"dependencies": {
"cson-parser": "^1.1.0",
"js-yaml": "^3.3.0",
"lodash": "^3.10.0"
},
"devDependencies": {
"grunt": "^0.4.0",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-watch": "^0.6.0",
"grunt-mocha-test": "^0.12.0",
"grunt-exec": "^0.4.6",
"spacejam": "^1.2.0"
},
"keywords": [
"replace",
"replacement",
"pattern",
"patterns",
"match",
"text",
"string",
"regex",
"regexp",
"json",
"yaml",
"cson",
"flatten"
],
"main": "src/applause.js",
"files": [
"src"
]
}
applause-1.2.2/scripts/ 0000775 0000000 0000000 00000000000 12577711720 0015026 5 ustar 00root root 0000000 0000000 applause-1.2.2/scripts/release.sh 0000775 0000000 0000000 00000000732 12577711720 0017007 0 ustar 00root root 0000000 0000000 #!/bin/sh
# bail immediately on error
set -e
# execute test files
npm test
CHANGES=$(git diff --numstat | wc -l)
CHANGES_CACHED=$(git diff --cached --numstat | wc -l)
TOTAL_CHANGES=$(($CHANGES + $CHANGES_CACHED))
# create the commit, tag the commit with the proper version
if [ $TOTAL_CHANGES -ne "0" ]
then
git add --all
git commit -am "Version $npm_package_version released."
fi
git tag $npm_package_version
git push
git push --tags
# publish to npm
npm publish
applause-1.2.2/src/ 0000775 0000000 0000000 00000000000 12577711720 0014126 5 ustar 00root root 0000000 0000000 applause-1.2.2/src/applause.js 0000664 0000000 0000000 00000011773 12577711720 0016307 0 ustar 00root root 0000000 0000000
/*
* 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;
applause-1.2.2/src/plugins.js 0000664 0000000 0000000 00000001347 12577711720 0016152 0 ustar 00root root 0000000 0000000
/*
* applause
*
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/applause/blob/master/LICENSE-MIT
*/
// dependencies
var path = require('path');
// private
// var dir = path.join(__dirname, '/plugins');
var plugins = [
require('./plugins/yaml'),
require('./plugins/cson'),
require('./plugins/json')
];
// took plugins from folder
/* require('fs').readdirSync(dir).forEach(function (file) {
if (file.match(/.+\.js/g) !== null && file !== 'index.js') {
var plugin = require(path.join(dir, file));
plugins.push(plugin);
}
}); */
// priority sort
plugins.sort(function (a, b) {
return (a.priority || 0) - (b.priority || 0);
});
// expose
module.exports = plugins;
applause-1.2.2/src/plugins/ 0000775 0000000 0000000 00000000000 12577711720 0015607 5 ustar 00root root 0000000 0000000 applause-1.2.2/src/plugins/cson.js 0000664 0000000 0000000 00000001432 12577711720 0017107 0 ustar 00root root 0000000 0000000
/*
* applause
*
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/applause/blob/master/LICENSE-MIT
*/
// dependencies
var _ = require('lodash');
var CSON = require('cson-parser');
// expose
module.exports = {
name: 'cson',
priority: 10,
match: function (pattern, opts) {
var cson = pattern.cson;
var match = typeof cson !== 'undefined';
return match;
},
transform: function (pattern, opts, done) {
var cson = pattern.cson;
// function support
if (_.isFunction(cson)) {
cson.call(this, function (result) {
// override cson function with value
cson = result;
});
}
try {
done({
json: CSON.parse(cson)
});
} catch (e) {
done(e);
}
}
};
applause-1.2.2/src/plugins/json.js 0000664 0000000 0000000 00000002673 12577711720 0017126 0 ustar 00root root 0000000 0000000
/*
* applause
*
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/applause/blob/master/LICENSE-MIT
*/
// dependencies
var _ = require('lodash');
// private
var flatten = function (json, delimiter) {
var result = [];
var recurse = function (cur, prop) {
for (var key in cur) {
if (cur.hasOwnProperty(key)) {
var item = cur[key];
var match = prop ? prop + delimiter + key : key;
result.push({
match: match,
replacement: item,
expression: false
});
// deep scan
if (typeof item === 'object') {
recurse(item, prop ? prop + delimiter + key : key);
}
}
}
};
recurse(json);
return result;
};
// expose
module.exports = {
name: 'json',
priority: 20,
match: function (pattern, opts) {
var json = pattern.json;
var match = typeof json !== 'undefined';
return match;
},
transform: function (pattern, opts, done) {
var delimiter = opts.delimiter;
var json = pattern.json;
// function support
if (_.isFunction(json)) {
json.call(this, function (result) {
// override json function with value
json = result;
});
}
if (_.isPlainObject(json)) {
// replace json with flatten data
done(flatten(json, delimiter));
} else {
done(new Error('Unsupported type for json (plain object expected).'));
}
}
};
applause-1.2.2/src/plugins/yaml.js 0000664 0000000 0000000 00000001431 12577711720 0017106 0 ustar 00root root 0000000 0000000
/*
* applause
*
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/applause/blob/master/LICENSE-MIT
*/
// dependencies
var _ = require('lodash');
var YAML = require('js-yaml');
// expose
module.exports = {
name: 'yaml',
priority: 10,
match: function (pattern, opts) {
var yaml = pattern.yaml;
var match = typeof yaml !== 'undefined';
return match;
},
transform: function (pattern, opts, done) {
var yaml = pattern.yaml;
// function support
if (_.isFunction(yaml)) {
yaml.call(this, function (result) {
// override yaml function with value
yaml = result;
});
}
try {
done({
json: YAML.safeLoad(yaml)
});
} catch (e) {
done(e);
}
}
};
applause-1.2.2/test/ 0000775 0000000 0000000 00000000000 12577711720 0014316 5 ustar 00root root 0000000 0000000 applause-1.2.2/test/replace_test.js 0000664 0000000 0000000 00000043755 12577711720 0017344 0 ustar 00root root 0000000 0000000
/*
* applause
*
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/applause/blob/master/LICENSE-MIT
*/
'use strict';
// dependencies
var assert = require('assert');
var Applause = require('../src/applause');
// test
describe('core', function () {
var applause;
var expect;
var result;
it('should replace simple key with value', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: 'value'
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with empty value', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: ''
}
]
});
expect = '';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with value and return the detail', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: 'value'
}
],
detail: true
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
assert.equal(result.detail.length, 1);
done();
});
it('should replace simple key with value (replacement alternative)', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replace: 'value'
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with value using custom prefix', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: 'value'
}
],
prefix: '@replace:'
});
expect = 'value';
result = applause.replace('@replace:key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with value without prefix', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: 'value'
}
],
usePrefix: false
});
expect = 'value';
result = applause.replace('key');
assert.equal(result.content, expect);
done();
});
it('should replace multiple key-value pairs', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key-1',
replacement: 'value-1'
},
{
match: 'key-2',
replacement: 'value-2'
}
]
});
expect = 'value-1,value-2';
result = applause.replace('@@key-1,@@key-2');
assert.equal(result.content, expect);
done();
});
it('should replace multiple key-value pairs and return the detail', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key-1',
replacement: 'value-1'
},
{
match: 'key-2',
replacement: 'value-2'
}
],
detail: true
});
expect = 'value-1,value-2';
result = applause.replace('@@key-1,@@key-2');
assert.equal(result.content, expect);
assert.equal(result.detail.length, 2);
done();
});
it('should not escape dollar sequence when use simple key', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: '$'
}
]
});
expect = '$';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should escape dollar sequence when use regexp for matching', function (done) {
applause = Applause.create({
patterns: [
{
match: /key/g,
replacement: '$$'
}
]
});
expect = '$';
result = applause.replace('key');
assert.equal(result.content, expect);
done();
});
it('should use special characters in replacement', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: 'detta är en sträng'
}
]
});
expect = 'detta är en sträng';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should use regexp for matching', function (done) {
applause = Applause.create({
patterns: [
{
match: /key/g,
replacement: 'value'
}
]
});
expect = 'value';
result = applause.replace('key');
assert.equal(result.content, expect);
done();
});
it('should use function for replacements', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: function () {
return 'value';
}
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace "John Smith" for "Smith, John"', function (done) {
applause = Applause.create({
patterns: [
{
match: /(\w+)\s(\w+)/,
replacement: '$2, $1'
}
]
});
expect = 'Smith, John';
result = applause.replace('John Smith');
assert.equal(result.content, expect);
done();
});
it('should throw error when invalid match type found', function (done) {
applause = Applause.create({
patterns: [
{
match: 1,
replacement: 'value'
}
]
});
assert.throws(
function () {
applause.replace('@@1');
},
Error
);
done();
});
it('should throw error when match attribute was undefined', function (done) {
applause = Applause.create({
patterns: [
{
replacement: 'value'
}
]
});
assert.throws(
function () {
applause.replace('@@key');
},
Error
);
done();
});
it('should throw error when match attribute was null', function (done) {
applause = Applause.create({
patterns: [
{
match: null,
replacement: 'value'
}
]
});
assert.throws(
function () {
applause.replace('@@key');
},
Error
);
done();
});
it('should throw error when replacement attribute was undefined', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key'
}
]
});
assert.throws(
function () {
applause.replace('@@key');
},
Error
);
done();
});
it('should throw error when replacement attribute was null', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: null
}
]
});
assert.throws(
function () {
applause.replace('@@key');
},
Error
);
done();
});
it('should ignore empty patterns definitions', function (done) {
applause = Applause.create({
patterns: [
{
// pass
}
]
});
expect = false;
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should not match any pattern definition', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: 'value'
}
]
});
expect = false;
result = applause.replace();
assert.equal(result.content, expect);
done();
});
it('should replace patterns using plain objects', function (done) {
applause = Applause.create({
variables: {
key: 'value'
}
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with array object representation', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: [1, 2, 3, 4]
}
]
});
expect = '[1,2,3,4]';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with plain object representation', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: {
foo: 'bar'
}
}
]
});
expect = '{"foo":"bar"}';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should sort patterns to prevent bad replaces', function (done) {
applause = Applause.create({
patterns: [
{
match: 'smaller',
replacement: '2'
},
{
match: 'small',
replacement: '1'
},
{
match: 'smallest',
replacement: '3'
}
]
});
expect = '1-2-3';
result = applause.replace('@@small-@@smaller-@@smallest');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with value and preserve prefix', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: 'value'
}
],
preservePrefix: true
});
expect = '@@value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with array object representation and preserve prefix', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: [1, 2, 3, 4]
}
],
preservePrefix: true
});
expect = '@@[1,2,3,4]';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with value and not preserve prefix (function as replacement)', function (done) {
applause = Applause.create({
patterns: [
{
match: 'key',
replacement: function () {
return 'value';
}
}
],
preservePrefix: true
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should replace simple key with value and not preserve prefix (regexp as match)', function (done) {
applause = Applause.create({
patterns: [
{
match: /@@key/g,
replacement: 'value'
}
],
preservePrefix: true
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should escape string to create an regexp', function (done) {
applause = Applause.create({
patterns: [
{
match: '(../fonts/',
replacement: '../font/'
}
],
usePrefix: false
});
expect = '../font/';
result = applause.replace('(../fonts/');
assert.equal(result.content, expect);
done();
});
it('should escape special characters in replacement', function (done) {
applause = Applause.create({
patterns: [
{
match: '$var',
replacement: '$var-value'
},
{
match: '@var',
replacement: '@var-value'
}
],
usePrefix: false
});
expect = [
'$var-value',
'@var-value'
].join();
result = applause.replace([
'$var',
'@var'
].join());
assert.equal(result.content, expect);
done();
});
it('should escape special characters in replacement (function as replacement)', function (done) {
applause = Applause.create({
patterns: [
{
match: '$var-fn',
replacement: function () {
return '$var-fn-value';
}
},
{
match: '@var-fn',
replacement: function () {
return '@var-fn-value';
}
}
],
usePrefix: false
});
expect = [
'$var-fn-value',
'@var-fn-value'
].join();
result = applause.replace([
'$var-fn',
'@var-fn'
].join());
assert.equal(result.content, expect);
done();
});
it('should check version number', function (done) {
assert(typeof Applause.version !== 'undefined');
done();
});
});
describe('plugins', function () {
var applause;
var expect;
var result;
it('should read from json and replace simple key with value', function (done) {
applause = Applause.create({
patterns: [
{
json: {
key: 'value'
}
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should read from json and replace simple key with value (escape dollar sequence)', function (done) {
applause = Applause.create({
patterns: [
{
json: {
'API_KEY': '12213lkhgjhvj$$bvhmvff@sdfertvc'
}
}
]
});
expect = '12213lkhgjhvj$$bvhmvff@sdfertvc';
result = applause.replace('@@API_KEY');
assert.equal(result.content, expect);
done();
});
it('should read from deferrer json and replace simple key with value', function (done) {
applause = Applause.create({
patterns: [
{
json: function (done) {
done({
key: 'value'
});
}
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should read from yaml and replace simple key with value', function (done) {
applause = Applause.create({
patterns: [
{
yaml: 'key: value'
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should read from yaml and replace simple key with value (escape dollar sequence)', function (done) {
applause = Applause.create({
patterns: [
{
yaml: 'API_KEY: 12213lkhgjhvj$$bvhmvff@sdfertvc'
}
]
});
expect = '12213lkhgjhvj$$bvhmvff@sdfertvc';
result = applause.replace('@@API_KEY');
assert.equal(result.content, expect);
done();
});
it('should read from deferrer yaml and replace simple key with value', function (done) {
applause = Applause.create({
patterns: [
{
yaml: function (done) {
done('key: value');
}
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should read from cson and replace simple key with value', function (done) {
applause = Applause.create({
patterns: [
{
cson: 'key: \'value\''
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should read from cson and replace simple key with value (escape dollar sequence)', function (done) {
applause = Applause.create({
patterns: [
{
cson: 'API_KEY: \'12213lkhgjhvj$$bvhmvff@sdfertvc\''
}
]
});
expect = '12213lkhgjhvj$$bvhmvff@sdfertvc';
result = applause.replace('@@API_KEY');
assert.equal(result.content, expect);
done();
});
it('should read from deferrer cson and replace simple key with value', function (done) {
applause = Applause.create({
patterns: [
{
cson: function (done) {
done('key: \'value\'');
}
}
]
});
expect = 'value';
result = applause.replace('@@key');
assert.equal(result.content, expect);
done();
});
it('should sort the json patterns to prevent bad replaces', function (done) {
applause = Applause.create({
patterns: [
{
json: {
'small': '1',
'smaller': '2',
'smallest': '3'
}
}
]
});
expect = '1-2-3';
result = applause.replace('@@small-@@smaller-@@smallest');
assert.equal(result.content, expect);
done();
});
// alternative
var json_test = {
'key_1': 'value_1',
'key_2': 'value_2',
'group_1': {
'group_2': {
'key_3': 'value_3'
},
'group_3': {
'key_4': 'value_4'
},
'array': [1, 2, 3]
}
};
it('should flatten json object', function (done) {
applause = Applause.create({
patterns: [
{
json: json_test
}
]
});
expect = 'value_3';
result = applause.replace('@@group_1.group_2.key_3');
assert.equal(result.content, expect);
done();
});
it('should flatten json object with custom delimiter', function (done) {
applause = Applause.create({
patterns: [
{
json: json_test
}
],
delimiter: '-'
});
expect = 'value_3';
result = applause.replace('@@group_1-group_2-key_3');
assert.equal(result.content, expect);
done();
});
it('should replace simple key from json with plain object representation', function (done) {
applause = Applause.create({
patterns: [
{
json: json_test
}
]
});
expect = '{"key_3":"value_3"}';
result = applause.replace('@@group_1.group_2');
assert.equal(result.content, expect);
done();
});
it('should replace simple key from json with plain object representation and preserve prefix', function (done) {
applause = Applause.create({
patterns: [
{
json: json_test
}
],
preservePrefix: true
});
expect = '@@{"key_3":"value_3"}';
result = applause.replace('@@group_1.group_2');
assert.equal(result.content, expect);
done();
});
});