ffffng/app/bower_components/lodash/lib/fp/build-doc.js

79 lines
2 KiB
JavaScript
Raw Permalink Normal View History

2016-05-16 13:33:49 +02:00
'use strict';
2017-05-13 13:25:33 +02:00
const _ = require('lodash');
const fs = require('fs-extra');
const path = require('path');
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const file = require('../common/file');
const mapping = require('../common/mapping');
const util = require('../common/util');
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const templatePath = path.join(__dirname, 'template/doc');
const template = file.globTemplate(path.join(templatePath, '*.jst'));
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const argNames = ['a', 'b', 'c', 'd'];
2016-05-16 13:33:49 +02:00
2017-05-13 13:25:33 +02:00
const templateData = {
mapping,
toArgOrder,
toFuncList
2016-05-16 13:33:49 +02:00
};
2017-05-13 13:25:33 +02:00
/**
* Converts arranged argument `indexes` into a named argument string
* representation of their order.
*
* @private
* @param {number[]} indexes The arranged argument indexes.
* @returns {string} Returns the named argument string.
*/
function toArgOrder(indexes) {
const reordered = [];
_.each(indexes, (newIndex, index) => {
2016-05-16 13:33:49 +02:00
reordered[newIndex] = argNames[index];
});
return '`(' + reordered.join(', ') + ')`';
}
2017-05-13 13:25:33 +02:00
/**
* Converts `funcNames` into a chunked list string representation.
*
* @private
* @param {string[]} funcNames The function names.
* @returns {string} Returns the function list string.
*/
function toFuncList(funcNames) {
let chunks = _.chunk(funcNames.slice().sort(), 5);
let lastChunk = _.last(chunks);
const lastName = lastChunk ? lastChunk.pop() : undefined;
2016-05-16 13:33:49 +02:00
chunks = _.reject(chunks, _.isEmpty);
lastChunk = _.last(chunks);
2017-05-13 13:25:33 +02:00
let result = '`' + _.map(chunks, chunk => chunk.join('`, `') + '`').join(',\n`');
if (lastName == null) {
2016-05-16 13:33:49 +02:00
return result;
}
if (_.size(chunks) > 1 || _.size(lastChunk) > 1) {
result += ',';
}
result += ' &';
result += _.size(lastChunk) < 5 ? ' ' : '\n';
2017-05-13 13:25:33 +02:00
return result + '`' + lastName + '`';
2016-05-16 13:33:49 +02:00
}
/*----------------------------------------------------------------------------*/
2017-05-13 13:25:33 +02:00
/**
* Creates the FP-Guide wiki at the `target` path.
*
* @private
* @param {string} target The output file path.
*/
2016-05-16 13:33:49 +02:00
function build(target) {
target = path.resolve(target);
2017-05-13 13:25:33 +02:00
fs.writeFile(target, template.wiki(templateData), util.pitch);
2016-05-16 13:33:49 +02:00
}
build(_.last(process.argv));