From f87ec1c6db0a8dc6e3d5c19f2d9f4af1434b2f65 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 6 Jun 2018 14:31:33 +0200 Subject: [PATCH] Setup Babel compilation (WIP) --- .babelrc | 6 + bin/cli.js | 2 +- bin/repl.js | 2 +- gulpfile.js | 15 +- lib/utils/array.js | 284 ++++--- package-lock.json | 828 ++++++++++++++++++++ package.json | 25 +- test/function/arithmetic/divide.test.js | 18 +- test/function/arithmetic/unaryMinus.test.js | 22 +- test/function/arithmetic/unaryPlus.test.js | 12 +- test/function/bitwise/bitNot.test.js | 10 +- test/function/matrix/expm.test.js | 8 +- test/function/matrix/kron.test.js | 10 +- test/type/complex/Complex.test.js | 4 +- test/type/unit/Unit.test.js | 76 +- test/utils/bignumber/formatter.test.js | 14 +- tools/validate.js | 40 +- 17 files changed, 1111 insertions(+), 265 deletions(-) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 000000000..620a8fbc5 --- /dev/null +++ b/.babelrc @@ -0,0 +1,6 @@ +{ + "presets": [ + ["env"] + ], + "plugins": ["transform-object-assign"] +} \ No newline at end of file diff --git a/bin/cli.js b/bin/cli.js index 8811137a0..d640ae8c4 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -57,7 +57,7 @@ var PRECISION = 14; // decimals * @return {*} */ function getMath () { - return require('../index'); + return require('../dist/math.js'); } /** diff --git a/bin/repl.js b/bin/repl.js index 8cd111dda..369338e0d 100755 --- a/bin/repl.js +++ b/bin/repl.js @@ -4,7 +4,7 @@ * This simply preloads mathjs and drops you into a REPL to * help interactive debugging. **/ -math = require('../index'); +math = require('../dist/math'); var repl = require('repl'); repl.start({useGlobal: true}); diff --git a/gulpfile.js b/gulpfile.js index 2e13d33e4..afd18ee9b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -61,6 +61,15 @@ var webpackConfig = { // new webpack.optimize.ModuleConcatenationPlugin() // TODO: ModuleConcatenationPlugin seems not to work. https://medium.com/webpack/webpack-3-official-release-15fd2dd8f07b ], + module: { + rules: [ + { + test: /\.js$/, + exclude: /node_modules/, + use: "babel-loader" + } + ] + }, optimization: { minimize: false }, @@ -80,7 +89,7 @@ var uglifyConfig = { // create a single instance of the compiler to allow caching var compiler = webpack(webpackConfig); -gulp.task('bundle', ['validate'], function (cb) { +gulp.task('bundle', [], function (cb) { // update the banner contents (has a date in it which should stay up to date) bannerPlugin.banner = createBanner(); @@ -123,7 +132,7 @@ gulp.task('minify', ['bundle'], function () { }); // test whether the docs for the expression parser are complete -gulp.task('validate', function (cb) { +gulp.task('validate', ['minify'], function (cb) { var child_process = require('child_process'); // this is run in a separate process as the modules need to be reloaded @@ -149,4 +158,4 @@ gulp.task('watch', ['bundle'], function () { }); // The default task (called when you run `gulp`) -gulp.task('default', ['bundle', 'minify', 'docs']); +gulp.task('default', ['bundle', 'minify', 'validate', 'docs']); diff --git a/lib/utils/array.js b/lib/utils/array.js index 910e4242b..7e364176b 100644 --- a/lib/utils/array.js +++ b/lib/utils/array.js @@ -1,10 +1,10 @@ -'use strict'; +'use strict' -var number = require('./number'); -var string = require('./string'); +import number from './number' +import string from './string' -var DimensionError = require('../error/DimensionError'); -var IndexError = require('../error/IndexError'); +import DimensionError from '../error/DimensionError' +import IndexError from'../error/IndexError' /** * Calculate the size of a multi dimensional array. @@ -13,16 +13,16 @@ var IndexError = require('../error/IndexError'); * @param {Array} x * @Return {Number[]} size */ -exports.size = function (x) { - var s = []; +export function size(x) { + let s = [] while (Array.isArray(x)) { - s.push(x.length); - x = x[0]; + s.push(x.length) + x = x[0] } - return s; -}; + return s +} /** * Recursively validate whether each element in a multi dimensional array @@ -34,29 +34,29 @@ exports.size = function (x) { * @private */ function _validate(array, size, dim) { - var i; - var len = array.length; + let i + const len = array.length - if (len != size[dim]) { - throw new DimensionError(len, size[dim]); + if (len !== size[dim]) { + throw new DimensionError(len, size[dim]) } if (dim < size.length - 1) { // recursively validate each child array - var dimNext = dim + 1; + const dimNext = dim + 1 for (i = 0; i < len; i++) { - var child = array[i]; + const child = array[i] if (!Array.isArray(child)) { - throw new DimensionError(size.length - 1, size.length, '<'); + throw new DimensionError(size.length - 1, size.length, '<') } - _validate(array[i], size, dimNext); + _validate(array[i], size, dimNext) } } else { // last dimension. none of the childs may be an array for (i = 0; i < len; i++) { if (Array.isArray(array[i])) { - throw new DimensionError(size.length + 1, size.length, '>'); + throw new DimensionError(size.length + 1, size.length, '>') } } } @@ -69,19 +69,19 @@ function _validate(array, size, dim) { * @param {number[]} size Array with the size of each dimension * @throws DimensionError */ -exports.validate = function(array, size) { - var isScalar = (size.length == 0); +export function validate(array, size) { + const isScalar = (size.length === 0) if (isScalar) { // scalar if (Array.isArray(array)) { - throw new DimensionError(array.length, 0); + throw new DimensionError(array.length, 0) } } else { // array - _validate(array, size, 0); + _validate(array, size, 0) } -}; +} /** * Test whether index is an integer number with index >= 0 and index < length @@ -91,12 +91,12 @@ exports.validate = function(array, size) { */ exports.validateIndex = function(index, length) { if (!number.isNumber(index) || !number.isInteger(index)) { - throw new TypeError('Index must be an integer (value: ' + index + ')'); + throw new TypeError('Index must be an integer (value: ' + index + ')') } if (index < 0 || (typeof length === 'number' && index >= length)) { - throw new IndexError(index, length); + throw new IndexError(index, length) } -}; +} /** * Resize a multi dimensional array. The resized array is returned. @@ -108,31 +108,31 @@ exports.validateIndex = function(index, length) { * set. * @return {Array} array The resized array */ -exports.resize = function(array, size, defaultValue) { +export function resize(array, size, defaultValue) { // TODO: add support for scalars, having size=[] ? // check the type of the arguments if (!Array.isArray(array) || !Array.isArray(size)) { - throw new TypeError('Array expected'); + throw new TypeError('Array expected') } if (size.length === 0) { - throw new Error('Resizing to scalar is not supported'); + throw new Error('Resizing to scalar is not supported') } // check whether size contains positive integers size.forEach(function (value) { if (!number.isNumber(value) || !number.isInteger(value) || value < 0) { throw new TypeError('Invalid size, must contain positive integers ' + - '(size: ' + string.format(size) + ')'); + '(size: ' + string.format(size) + ')') } - }); + }) // recursively resize the array - var _defaultValue = (defaultValue !== undefined) ? defaultValue : 0; - _resize(array, size, 0, _defaultValue); + const _defaultValue = (defaultValue !== undefined) ? defaultValue : 0 + _resize(array, size, 0, _defaultValue) - return array; -}; + return array +} /** * Recursively resize a multi dimensional array @@ -144,38 +144,38 @@ exports.resize = function(array, size, defaultValue) { * @private */ function _resize (array, size, dim, defaultValue) { - var i; - var elem; - var oldLen = array.length; - var newLen = size[dim]; - var minLen = Math.min(oldLen, newLen); + let i + let elem + const oldLen = array.length + const newLen = size[dim] + const minLen = Math.min(oldLen, newLen) // apply new length - array.length = newLen; + array.length = newLen if (dim < size.length - 1) { // non-last dimension - var dimNext = dim + 1; + const dimNext = dim + 1 // resize existing child arrays for (i = 0; i < minLen; i++) { // resize child array - elem = array[i]; + elem = array[i] if (!Array.isArray(elem)) { elem = [elem]; // add a dimension - array[i] = elem; + array[i] = elem } - _resize(elem, size, dimNext, defaultValue); + _resize(elem, size, dimNext, defaultValue) } // create new child arrays for (i = minLen; i < newLen; i++) { // get child array - elem = []; - array[i] = elem; + elem = [] + array[i] = elem // resize new child array - _resize(elem, size, dimNext, defaultValue); + _resize(elem, size, dimNext, defaultValue) } } else { @@ -184,13 +184,13 @@ function _resize (array, size, dim, defaultValue) { // remove dimensions of existing values for (i = 0; i < minLen; i++) { while (Array.isArray(array[i])) { - array[i] = array[i][0]; + array[i] = array[i][0] } } // fill new elements with the default value for (i = minLen; i < newLen; i++) { - array[i] = defaultValue; + array[i] = defaultValue } } } @@ -205,35 +205,33 @@ function _resize (array, size, dim, defaultValue) { * @throws {DimensionError} If the product of the new dimension sizes does * not equal that of the old ones */ -exports.reshape = function(array, sizes) { - var flatArray = exports.flatten(array); - var newArray; +export function reshape(array, sizes) { + const flatArray = exports.flatten(array) + let newArray - var product = function (arr) { - return arr.reduce(function (prev, curr) { - return prev * curr; - }); - }; + function product(arr) { + return arr.reduce((prev, curr) => prev * curr) + } if (!Array.isArray(array) || !Array.isArray(sizes)) { - throw new TypeError('Array expected'); + throw new TypeError('Array expected') } if (sizes.length === 0) { - throw new DimensionError(0, product(exports.size(array)), '!='); + throw new DimensionError(0, product(exports.size(array)), '!=') } try { - newArray = _reshape(flatArray, sizes); + newArray = _reshape(flatArray, sizes) } catch (e) { if (e instanceof DimensionError) { throw new DimensionError( product(sizes), product(exports.size(array)), '!=' - ); + ) } - throw e; + throw e } if (flatArray.length > 0) { @@ -241,11 +239,11 @@ exports.reshape = function(array, sizes) { product(sizes), product(exports.size(array)), '!=' - ); + ) } - return newArray; -}; + return newArray +} /** * Recursively re-shape a multi dimensional array to fit the specified dimensions @@ -258,19 +256,19 @@ exports.reshape = function(array, sizes) { * not equal that of the old ones */ function _reshape(array, sizes) { - var accumulator = []; - var i; + let accumulator = [] + let i if (sizes.length === 0) { if (array.length === 0) { - throw new DimensionError(null, null, '!='); + throw new DimensionError(null, null, '!=') } - return array.shift(); + return array.shift() } for (i = 0; i < sizes[0]; i += 1) { - accumulator.push(_reshape(array, sizes.slice(1))); + accumulator.push(_reshape(array, sizes.slice(1))) } - return accumulator; + return accumulator } @@ -281,28 +279,28 @@ function _reshape(array, sizes) { * @returns {Array} returns the array itself */ exports.squeeze = function(array, size) { - var s = size || exports.size(array); + let s = size || exports.size(array) // squeeze outer dimensions while (Array.isArray(array) && array.length === 1) { - array = array[0]; - s.shift(); + array = array[0] + s.shift() } // find the first dimension to be squeezed - var dims = s.length; + let dims = s.length while (s[dims - 1] === 1) { - dims--; + dims-- } // squeeze inner dimensions if (dims < s.length) { - array = _squeeze(array, dims, 0); - s.length = dims; + array = _squeeze(array, dims, 0) + s.length = dims } - return array; -}; + return array +} /** * Recursively squeeze a multi dimensional array @@ -313,21 +311,21 @@ exports.squeeze = function(array, size) { * @private */ function _squeeze (array, dims, dim) { - var i, ii; + let i, ii if (dim < dims) { - var next = dim + 1; + const next = dim + 1 for (i = 0, ii = array.length; i < ii; i++) { - array[i] = _squeeze(array[i], dims, next); + array[i] = _squeeze(array[i], dims, next) } } else { while (Array.isArray(array)) { - array = array[0]; + array = array[0] } } - return array; + return array } /** @@ -342,25 +340,25 @@ function _squeeze (array, dims, dim) { * @returns {Array} returns the array itself * @private */ -exports.unsqueeze = function(array, dims, outer, size) { - var s = size || exports.size(array); +export function unsqueeze(array, dims, outer, size) { + let s = size || exports.size(array) // unsqueeze outer dimensions if (outer) { - for (var i = 0; i < outer; i++) { - array = [array]; - s.unshift(1); + for (let i = 0; i < outer; i++) { + array = [array] + s.unshift(1) } } // unsqueeze inner dimensions - array = _unsqueeze(array, dims, 0); + array = _unsqueeze(array, dims, 0) while (s.length < dims) { - s.push(1); + s.push(1) } - return array; -}; + return array +} /** * Recursively unsqueeze a multi dimensional array @@ -371,21 +369,21 @@ exports.unsqueeze = function(array, dims, outer, size) { * @private */ function _unsqueeze (array, dims, dim) { - var i, ii; + let i, ii if (Array.isArray(array)) { - var next = dim + 1; + const next = dim + 1 for (i = 0, ii = array.length; i < ii; i++) { - array[i] = _unsqueeze(array[i], dims, next); + array[i] = _unsqueeze(array[i], dims, next) } } else { - for (var d = dim; d < dims; d++) { - array = [array]; + for (let d = dim; d < dims; d++) { + array = [array] } } - return array; + return array } /** * Flatten a multi dimensional array, put all elements in a one dimensional @@ -393,32 +391,32 @@ function _unsqueeze (array, dims, dim) { * @param {Array} array A multi dimensional array * @return {Array} The flattened array (1 dimensional) */ -exports.flatten = function(array) { +export function flatten(array) { if (!Array.isArray(array)) { //if not an array, return as is - return array; + return array } - var flat = []; + let flat = [] array.forEach(function callback(value) { if (Array.isArray(value)) { value.forEach(callback); //traverse through sub-arrays recursively } else { - flat.push(value); + flat.push(value) } - }); + }) - return flat; -}; + return flat +} /** * A safe map * @param {Array} array * @param {function} callback */ -exports.map = function (array, callback) { - return Array.prototype.map.call(array, callback); +export function map(array, callback) { + return Array.prototype.map.call(array, callback) } /** @@ -426,8 +424,8 @@ exports.map = function (array, callback) { * @param {Array} array * @param {function} callback */ -exports.forEach = function (array, callback) { - Array.prototype.forEach.call(array, callback); +export function forEach(array, callback) { + Array.prototype.forEach.call(array, callback) } /** @@ -435,12 +433,12 @@ exports.forEach = function (array, callback) { * @param {Array} array * @param {function} callback */ -exports.filter = function (array, callback) { +export function filter(array, callback) { if (exports.size(array).length !== 1) { - throw new Error('Only one dimensional matrices supported'); + throw new Error('Only one dimensional matrices supported') } - return Array.prototype.filter.call(array, callback); + return Array.prototype.filter.call(array, callback) } /** @@ -450,14 +448,12 @@ exports.filter = function (array, callback) { * @return {Array} Returns the filtered array * @private */ -exports.filterRegExp = function (array, regexp) { +export function filterRegExp(array, regexp) { if (exports.size(array).length !== 1) { - throw new Error('Only one dimensional matrices supported'); + throw new Error('Only one dimensional matrices supported') } - return Array.prototype.filter.call(array, function (entry) { - return regexp.test(entry); - }); + return Array.prototype.filter.call(array, (entry) => regexp.test(entry)) } /** @@ -465,8 +461,8 @@ exports.filterRegExp = function (array, regexp) { * @param {Array} array * @param {string} separator */ -exports.join = function (array, separator) { - return Array.prototype.join.call(array, separator); +export function join(array, separator) { + return Array.prototype.join.call(array, separator) } /** @@ -474,49 +470,49 @@ exports.join = function (array, separator) { * @param {Array} a An array * @return {Array} An array of objects containing the original value and its identifier */ -exports.identify = function(a) { +export function identify(a) { if (!Array.isArray(a)) { - throw new TypeError('Array input expected'); + throw new TypeError('Array input expected') } if (a.length === 0) { - return a; + return a } - var b = []; - var count = 0; - b[0] = {value: a[0], identifier: 0}; - for (var i=1; i