Added type checks to the jake-utils

This commit is contained in:
josdejong 2013-02-21 17:31:04 +01:00
parent a91169e098
commit b7b3436982
4 changed files with 70 additions and 20 deletions

View File

@ -4,8 +4,8 @@
* matrices, strings, and a large set of functions and constants.
* https://github.com/josdejong/mathjs
*
* @version 2013-02-20
* @date 0.2.0-SNAPSHOT
* @version @@version
* @date @@date
*
* @license
* Copyright (C) 2013 Jos de Jong <wjosdejong@gmail.com>

4
math.min.js vendored
View File

@ -4,8 +4,8 @@
* matrices, strings, and a large set of functions and constants.
* https://github.com/josdejong/mathjs
*
* @version 2013-02-20
* @date 0.2.0-SNAPSHOT
* @version @@version
* @date @@date
*
* @license
* Copyright (C) 2013 Jos de Jong <wjosdejong@gmail.com>

View File

@ -30,7 +30,7 @@
"jake": ">= 0.5.8",
"uglify-js": ">= 2.2.5",
"nodeunit": ">= 0.7.4",
"date-utils": ">= 1.2.12",
"dateable": ">= 0.1.2",
"filesize": ">= 1.7.9"
},
"scripts": {

View File

@ -25,9 +25,8 @@
var fs = require('fs'),
jake = require('jake'),
uglify = require('uglify-js');
require('date-utils');
uglify = require('uglify-js'),
dateable = require('dateable');
/**
* Returns today's date as a formatted string.
@ -37,13 +36,14 @@ require('date-utils');
*/
function today (format) {
var date = new Date();
return date.toFormat(format || 'YYYY-MM-DD');
return dateable.format(date, format || 'YYYY-MM-DD');
}
/**
* Read the version number from the package.json file.
* If not found, an error is thrown
* @return {String} version
* @throws {Error}
*/
function version() {
var pkg = JSON.parse(read('./package.json'));
@ -74,21 +74,49 @@ function version() {
* parameters:
* {String | RegExp} pattern
* {String} replacement
* {String[]} src The filenames. Can contain
* {String | String[]} src The filenames. Can contain
* patterns.
* @return {Object} res Result information. The object contains:
* {String[]} src List with the filenames on which
* the replacement is executed
* @throws {Error}
*/
// TODO: change params of src such that we can also use regex expressions
function replace (params) {
// do some checks on the provided parameters
if (!(params instanceof Object)) {
throw new Error('Object with parameters expected as first argument.');
}
if (!params.replacements) {
throw new Error('Parameter "replacements" missing.');
}
if (!(params.replacements instanceof Array)) {
throw new Error('Parameter "replacements" must be an array.');
}
if (!params.src) {
throw new Error('Parameter "src" containing an array with filenames missing.');
}
var filelist = new jake.FileList();
filelist.include(params.src);
var filenames = filelist.toArray();
filenames.forEach(function (filename) {
var file = String(read(filename));
params.replacements.forEach(function (replacement) {
params.replacements.forEach(function (replacement, index) {
// check the replacement parameters
if (!(replacement instanceof Object)) {
throw new Error('Parameter "replacement" must be an object.');
}
if (!replacement.pattern) {
throw new Error('Parameter "pattern" in missing replacement object ' +
'(index ' + index + ')');
}
if (!replacement.replacement) {
throw new Error('Parameter "replacement" missing in replacement object ' +
'(index ' + index + ')');
}
file = file.replace(replacement.pattern, replacement.replacement);
});
write(filename, file);
@ -130,13 +158,20 @@ function replace (params) {
* file
*/
function concat (params) {
// concatenate all source files
// do some checks on the provided parameters
if (!(params instanceof Object)) {
throw new Error('Object with parameters expected as first argument.');
}
if (!params.src) {
throw new Error('Parameter "src" containing an array with filenames missing.');
}
var code = '';
var separator = params.separator || '';
var separator = String(params.separator) || '';
// header
if (params.header) {
code += params.header + separator;
code += String(params.header) + separator;
}
// files
@ -148,8 +183,8 @@ function concat (params) {
});
// footer
if (params.header) {
code += params.footer;
if (params.footer) {
code += String(params.footer);
}
// write output
@ -182,6 +217,7 @@ function concat (params) {
* minified. The file names
* can contain patterns.
* {String} [dest] The target file. Optional
* {Object} [options] uglify-js options.
* {String} [header] Text to be added on top.
* Optional.
* {String} [separator] Text to be inserted between
@ -196,24 +232,38 @@ function concat (params) {
* file.
*/
function minify (params) {
// do some checks on the provided parameters
if (!(params instanceof Object)) {
throw new Error('Object with parameters expected as first argument.');
}
if (!params.src) {
throw new Error('Parameter "src" containing an array with filenames missing.');
}
if (params.options) {
if (!(params.options instanceof Object)) {
throw new Error('Parameter "options" must be an object.');
}
}
var code = '';
var separator = params.separator || '';
var separator = String(params.separator) || '';
var options = params.options || {};
// header
if (params.header) {
code += params.header + params.separator;
code += String(params.header) + separator;
}
// src
var filelist = new jake.FileList();
filelist.include(params.src);
var filenames = filelist.toArray();
var minified = uglify.minify(filenames, {});
var minified = uglify.minify(filenames, options);
code += minified.code;
// footer
if (params.footer) {
code += params.separator + params.footer;
code += separator + String(params.footer);
}
// write output