1 | 'use strict'
|
2 |
|
3 | const flatten = require('../../utils/array').flatten
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | const compareNatural = load(require('../relational/compareNatural'))
|
7 |
|
8 | |
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const setSize = typed('setSize', {
|
30 | 'Array | Matrix': function (a) {
|
31 | return Array.isArray(a) ? flatten(a).length : flatten(a.toArray()).length
|
32 | },
|
33 | 'Array | Matrix, boolean': function (a, unique) {
|
34 | if (unique === false || a.length === 0) {
|
35 | return Array.isArray(a) ? flatten(a).length : flatten(a.toArray()).length
|
36 | } else {
|
37 | const b = flatten(Array.isArray(a) ? a : a.toArray()).sort(compareNatural)
|
38 | let count = 1
|
39 | for (let i = 1; i < b.length; i++) {
|
40 | if (compareNatural(b[i], b[i - 1]) !== 0) {
|
41 | count++
|
42 | }
|
43 | }
|
44 | return count
|
45 | }
|
46 | }
|
47 | })
|
48 |
|
49 | return setSize
|
50 | }
|
51 |
|
52 | exports.name = 'setSize'
|
53 | exports.factory = factory
|