From 343b6cdcd497a810aafcf551fcd623215faa1486 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Mon, 22 Jul 2013 20:37:15 +0200 Subject: [PATCH] `serialize` option, for custom args serialization --- lib/_base.js | 2 +- lib/index.js | 3 ++- lib/primitive.js | 10 +++++++--- test/index.js | 16 +++++++++++++++- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/_base.js b/lib/_base.js index 3658a3a..ab11416 100644 --- a/lib/_base.js +++ b/lib/_base.js @@ -33,7 +33,7 @@ module.exports = exports = function (core) { length = (options.length === false) ? false : (options.length >>> 0); } - core(conf, length); + core(conf, length, options); forEach(ext, function (fn, name) { if (fn.force) fn(conf, options); diff --git a/lib/index.js b/lib/index.js index 00bc70c..157459e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,5 +18,6 @@ require('./ext/max'); module.exports = function (fn/* options */) { var options = Object(arguments[1]); - return call.call(options.primitive ? primitive : regular, this, fn, options); + return call.call((options.primitive || options.serialize) ? + primitive : regular, this, fn, options); }; diff --git a/lib/primitive.js b/lib/primitive.js index b9ae08b..d285133 100644 --- a/lib/primitive.js +++ b/lib/primitive.js @@ -3,6 +3,7 @@ 'use strict'; var CustomError = require('es5-ext/lib/Error/custom') + , callable = require('es5-ext/lib/Object/valid-callable') , hasListeners = require('event-emitter/lib/has-listeners') , serialize0 = function () { return ''; } @@ -22,11 +23,14 @@ serializeN = function (args) { return id; }; -module.exports = require('./_base')(function (conf, length) { +module.exports = require('./_base')(function (conf, length, options) { var get, cache = conf.cache = {}, fn - , hitListeners, initListeners, purgeListeners; + , hitListeners, initListeners, purgeListeners, serialize; - if (length === 1) { + if (options.serialize) { + serialize = callable(options.serialize); + get = conf.get = function (args) { return serialize.apply(this, args); }; + } else if (length === 1) { get = conf.get = serialize1; } else if (length === false) { get = conf.get = serializeN; diff --git a/test/index.js b/test/index.js index 8a38942..953c04f 100644 --- a/test/index.js +++ b/test/index.js @@ -1,7 +1,9 @@ 'use strict'; var toArray = require('es5-ext/lib/Array/from') - , nextTick = require('next-tick'); + , nextTick = require('next-tick') + + , join = Array.prototype.join; module.exports = function (t, a) { return { @@ -88,6 +90,18 @@ module.exports = function (t, a) { } }; }, + "Serialize": function () { + var i = 0, fn = function () { ++i; return join.call(arguments, '|'); } + , mfn; + mfn = t(fn, { serialize: Boolean }); + a(mfn(false, 'raz'), 'false|raz', "#1"); + a(mfn(0, 'dwa'), 'false|raz', "#2"); + a(i, 1, "Called once"); + a(mfn(34, 'bar'), '34|bar', "#3"); + a(i, 2, "Called twice"); + a(mfn(true, 'ola'), '34|bar', "#4"); + a(i, 2, "Called twice #2"); + }, "Dynamic": function () { var i = 0, fn = function () { ++i; return arguments; }, r;