diff options
| author | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
|---|---|---|
| committer | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
| commit | 60867fb030bae582082340ead7dbc7efdc2f5398 (patch) | |
| tree | 4c6a7356351be2e4914e15c4703172597c45656e /node_modules/textr/test | |
| parent | commenting (diff) | |
| download | s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.tar.xz s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.zip | |
2020/04/03, 02:34, v1.2.0
Diffstat (limited to 'node_modules/textr/test')
| -rw-r--r-- | node_modules/textr/test/bench.js | 65 | ||||
| -rw-r--r-- | node_modules/textr/test/index.js | 130 | ||||
| -rw-r--r-- | node_modules/textr/test/support.js | 28 |
3 files changed, 223 insertions, 0 deletions
diff --git a/node_modules/textr/test/bench.js b/node_modules/textr/test/bench.js new file mode 100644 index 0000000..9907e14 --- /dev/null +++ b/node_modules/textr/test/bench.js @@ -0,0 +1,65 @@ +#!/usr/bin/env node + +'use strict'; + +// dependencies +var textr = require('../'); +var program = require('commander'); +var pkg = require('../package'); + +/** + * Simple benchmark which can be used as CLI in the future. + * + * @example + * + * $ echo 'hello "world"' > test + * $ cat test | bench.js -t typographic-single-spaces,typographic-quotes + * + * hello "world" + * 1654434 cycles per second% + * + * + * + */ +program + .version(pkg.version) + .option('-c, --cycles', 'create new tf on every cycle') + .option('-t, --transforms [modules]', 'add transform functions to the bench') +; + +program.parse(process.argv); + +var tfs = []; +if (program.transforms) { + tfs = program.transforms + .split(',') + .map(function(tf) { + return require(tf); + }); +} + +var buf = ''; + +process.stdin.setEncoding('utf8'); +process.stdin.on('data', function(chunk) { + buf += chunk; +}); +process.stdin.on('end', bench); + + +function bench() { + var res; + var startedAt = +new Date(); + var endedAt = startedAt + 1000; + var i = 0; + var tf; + if (!program.cycles) { tf = textr().use.apply(null, tfs); } + do { + if (program.cycles) { tf = textr().use.apply(null, tfs); } + res = tf(buf); + i++; + } while (+new Date() < endedAt); + process.stdout.write(res); + process.stdout.write('\r\n'); + process.stdout.write(i + ' cycles per second'); +} diff --git a/node_modules/textr/test/index.js b/node_modules/textr/test/index.js new file mode 100644 index 0000000..c9a308e --- /dev/null +++ b/node_modules/textr/test/index.js @@ -0,0 +1,130 @@ +/* global describe, it, before, navigator */ + +'use strict'; + +var textr = require('..'); +var should = require('should'); +var noop = require('./support').noop; +var capitalize = require('./support').capitalize; +var headline = require('./support').headline; + +describe('textr', function() { + + describe('api', function() { + + it('should register transformers', function() { + var tf = textr(); + tf.should.have.property('use'); + tf.use.should.be.a.Function; + }); + + it('should process text', function() { + var tf = textr(); + tf.should.be.a.Function; + tf.should.have.property('exec'); + tf.exec.should.be.a.Function; + tf('hello').should.be.equal('hello'); + }); + + }); + + describe('use', function() { + + it('should return transformer', function() { + var tf = textr().use(noop); + tf.should.be.a.Function; + tf.use.should.be.a.Function; + }); + + it('should use plugins', function() { + textr() + .use(noop, noop) + .use(noop) + .mws.should.has.length(3) + ; + }); + + it('should use itself as transform', function() { + var one = textr() + .use(capitalize) + ; + var two = textr() + .use(headline(1)) + ; + var tf = textr() + .use(one, two) + .exec('hello world') + .should.be.equal('<h1>Hello World</h1>') + ; + }); + + it('should support string.prototype methods', function() { + var tf = textr() + .use(String.prototype.trim) + ; + tf('text\t\n').should.be.equal('text'); + }); + + }); + + describe('exec', function() { + + it('should apply transformers to given text', function() { + textr() + .use(capitalize) + .exec('hello world') + .should.be.equal('Hello World') + ; + }); + + it('should apply transformers with order of registration', function() { + textr() + .use(capitalize) + .use(headline(1)) + .exec('hello world') + .should.be.equal('<h1>Hello World</h1>') + ; + }); + + it('should not miss text if on of the middlewares return falsy', function() { + textr() + .use(function (text, ctx) { + // define mw that not transforming text; + if ('undefined' === typeof navigator) { return; } + ctx.locale = navigator.locale; + }) + .exec('some text') + .should.be.equal('some text') + ; + }); + + it('should support options to correct recursive usage', function() { + var pluginWithDefaultOptions = textr({ locale: 'ru', ownprop: true }) + .use(function(text, options) { + options.locale.should.be.equal('en-us'); + options.direction.should.be.equal('ltr'); + options.ownprop.should.be.equal(true); + options.other.should.be.equal('redefine'); + }) + ; + var tf = textr({ locale: 'en-us', direction: 'ltr', other: 'param' }) + .use(pluginWithDefaultOptions) + .exec('text', { other: 'redefine' }) + ; + }); + }); + + describe('options', function() { + it('should pass options through each middleware', function() { + textr({ locale: 'ru' }) + .use(function(text, opts) { + return text + opts.locale; + }) + .exec('locale: ') + .should.be.equal('locale: ru') + ; + }); + }); + + +}); diff --git a/node_modules/textr/test/support.js b/node_modules/textr/test/support.js new file mode 100644 index 0000000..a530253 --- /dev/null +++ b/node_modules/textr/test/support.js @@ -0,0 +1,28 @@ +'use strict'; + +/** + * Convert text to capitalized string + * @param {String} text + * @return {String} + */ +exports.capitalize = function(text) { + return text.replace(/\b(.{1})/g, function (l) { + return l.toUpperCase(); + }); +}; + +/** + * Wrap text by html headline with given level + * @param {Number} level + * @return {Function} + */ +exports.headline = function(level) { + return function(text) { + return '<h' + level + '>' + text + '</h' + level + '>'; + }; +}; + +/** + * noop function + */ +exports.noop = function() {}; |