UNPKG

1.04 kBJavaScriptView Raw
1'use strict'
2
3const errorTransform = require('./error.transform').transform
4const isCollection = require('../../utils/collection/isCollection')
5
6/**
7 * Attach a transform function to math.min
8 * Adds a property transform containing the transform function.
9 *
10 * This transform changed the last `dim` parameter of function min
11 * from one-based to zero based
12 */
13function factory (type, config, load, typed) {
14 const min = load(require('../../function/statistics/min'))
15
16 return typed('min', {
17 '...any': function (args) {
18 // change last argument dim from one-based to zero-based
19 if (args.length === 2 && isCollection(args[0])) {
20 const dim = args[1]
21 if (type.isNumber(dim)) {
22 args[1] = dim - 1
23 } else if (type.isBigNumber(dim)) {
24 args[1] = dim.minus(1)
25 }
26 }
27
28 try {
29 return min.apply(null, args)
30 } catch (err) {
31 throw errorTransform(err)
32 }
33 }
34 })
35}
36
37exports.name = 'min'
38exports.path = 'expression.transform'
39exports.factory = factory