serialize option, for custom args serialization

This commit is contained in:
Mariusz Nowak
2013-07-22 20:37:15 +02:00
parent baae9f8d54
commit 343b6cdcd4
4 changed files with 25 additions and 6 deletions

View File

@@ -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);

View File

@@ -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);
};

View File

@@ -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;

View File

@@ -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;