Skip to content

Commit

Permalink
Add support for presets
Browse files Browse the repository at this point in the history
Closes GH-22.
  • Loading branch information
wooorm committed Feb 12, 2017
1 parent 758d968 commit f0427f4
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 19 deletions.
46 changes: 28 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ function unified() {
* * a matrix: list containing any of the above and
* matrices.
* * a processor: another processor to use all its
* plugins (except parser if there’s already one). */
* plugins (except parser if there’s already one).
* * a preset: an object with `plugins` (any of the
* above, optional), and `settings` (object, optional). */
function use(value) {
var args = slice.call(arguments, 0);
var params = args.slice(1);
Expand All @@ -141,26 +143,34 @@ function unified() {

assertConcrete('use', concrete);

/* Multiple attachers. */
if ('length' in value && !func(value)) {
index = -1;
length = value.length;

if (!func(value[0])) {
/* Matrix of things. */
while (++index < length) {
use(value[index]);
}
} else if (func(value[1])) {
/* List of things. */
while (++index < length) {
use.apply(null, [value[index]].concat(params));
if (!func(value)) {
/* Multiple attachers. */
if ('length' in value) {
index = -1;
length = value.length;

if (!func(value[0])) {
/* Matrix of things. */
while (++index < length) {
use(value[index]);
}
} else if (func(value[1])) {
/* List of things. */
while (++index < length) {
use.apply(null, [value[index]].concat(params));
}
} else {
/* Arguments. */
use.apply(null, value);
}
} else {
/* Arguments. */
use.apply(null, value);

return processor;
}

/* Preset. */
use(value.plugins || []);
namespace.settings = extend(namespace.settings || {}, value.settings || {});

return processor;
}

Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ unified()
.use(document)
.use(format)
.use(html)
.process(`# Hello world!`, function (err, file) {
.process('# Hello world!', function (err, file) {
console.error(report(err || file));
console.log(String(file));
});
Expand Down Expand Up @@ -244,6 +244,7 @@ that plug-in with optional options.
* `processor.use(plugins[, options])`;
* `processor.use(list)`;
* `processor.use(matrix)`;
* `processor.use(preset)`;
* `processor.use(processor)`.

###### Parameters
Expand All @@ -253,6 +254,9 @@ that plug-in with optional options.
* `plugins` (`Array.<Function>`) — List of plugins;
* `list` (`Array`) — `plugin` and `options` in an array;
* `matrix` (`Array`) — Array where each entry is a `list`;
* `preset` (`Object`) — Object with an optional `plugins`
(set to `plugins`, `list`, or `matrix`), and/or an optional
`settings` object;
* `processor` ([`Processor`][processor]) — Other processor whose
plugins to use (except for a parser).

Expand Down
135 changes: 135 additions & 0 deletions test/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,141 @@ test('use(plugin[, options])', function (t) {
t.end();
});

test('use(preset)', function (t) {
t.test('should support empty presets', function (st) {
var p = unified();

st.equal(p.use({}), p, 'should support empty presets (1)');
st.equal(p.attachers.length, 0, 'should support empty presets (2)');
st.end();
});

t.test('should support presets with empty plugins', function (st) {
var p = unified();
var preset = {plugins: []};

st.equal(p.use(preset), p, 'should support presets with empty plugins (1)');
st.equal(p.attachers.length, 0, 'should support presets with empty plugins (2)');
st.end();
});

t.test('should support presets with empty settings', function (st) {
var p = unified();
var preset = {settings: {}};

st.equal(p.use(preset), p, 'should support presets with empty settings (1)');
st.deepEqual(p.data(), {settings: {}}, 'should support presets with empty settings (2)');
st.end();
});

t.test('should support presets with a plugin', function (st) {
var p = unified();
var preset = {plugins: [plugin]};

st.plan(3);
st.equal(p.use(preset), p, 'should support presets with a plugin (1)');
st.equal(p.attachers.length, 1, 'should support presets with a plugin (2)');

function plugin() {
st.pass();
}
});

t.test('should support presets with plugins', function (st) {
var p = unified();
var preset = {plugins: [a, b]};

st.plan(4);
st.equal(p.use(preset), p, 'should support presets with plugins (1)');
st.equal(p.attachers.length, 2, 'should support presets with plugins (2)');

function a() {
st.pass();
}

function b() {
st.pass();
}
});

t.test('should support presets with settings', function (st) {
var p = unified();
var preset = {settings: {foo: true}};

st.equal(p.use(preset), p, 'should support presets with settings (1)');
st.deepEqual(p.data('settings'), {foo: true}, 'should support presets with settings (2)');
st.end();
});

t.test('should merge multiple presets with settings', function (st) {
var data = unified()
.use({settings: {foo: true, bar: true}})
.use({settings: {qux: true, foo: false}})
.data();

st.deepEqual(
data.settings,
{foo: false, bar: true, qux: true},
'should merge multiple presets with settings'
);

st.end();
});

t.test('should support extending presets', function (st) {
var p = unified().use({settings: {alpha: true}, plugins: [a, b]});
var q = p();

st.plan(7);
st.equal(p.attachers.length, 2, 'should support extending presets (1)');
st.equal(q.attachers.length, 2, 'should support extending presets (2)');
st.deepEqual(q.data('settings'), {alpha: true}, 'should support extending presets (3)');

function a() {
st.pass();
}

function b() {
st.pass();
}
});

t.test('should support presets with plugins as a tuple', function (st) {
var opts = {};
var p = unified().use({plugins: [plugin, opts]});
var q = p();

st.plan(4);
st.equal(p.attachers.length, 1, 'should support tuples (1)');
st.equal(q.attachers.length, 1, 'should support tuples (2)');

function plugin(options) {
st.equal(options, opts, 'should pass options to plugin');
}
});

t.test('should support presets with plugins as a matrix', function (st) {
var one = {};
var two = {};
var p = unified().use({plugins: [[a, one], [b, two]]});
var q = p();

st.plan(6);
st.equal(p.attachers.length, 2, 'should support matrices (1)');
st.equal(q.attachers.length, 2, 'should support matrices (2)');

function a(options) {
st.equal(options, one, 'should pass options to plugin (1)');
}

function b(options) {
st.equal(options, two, 'should pass options to plugin (2)');
}
});

t.end();
});

/* Processors. */
test('use(processor)', function (t) {
var p = unified();
Expand Down

0 comments on commit f0427f4

Please sign in to comment.