1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createOnes = void 0;
|
7 |
|
8 | var _is = require("../../utils/is.js");
|
9 |
|
10 | var _number = require("../../utils/number.js");
|
11 |
|
12 | var _array = require("../../utils/array.js");
|
13 |
|
14 | var _factory = require("../../utils/factory.js");
|
15 |
|
16 | var name = 'ones';
|
17 | var dependencies = ['typed', 'config', 'matrix', 'BigNumber'];
|
18 | var createOnes = (0, _factory.factory)(name, dependencies, function (_ref) {
|
19 | var typed = _ref.typed,
|
20 | config = _ref.config,
|
21 | matrix = _ref.matrix,
|
22 | BigNumber = _ref.BigNumber;
|
23 |
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | return typed('ones', {
|
58 | '': function _() {
|
59 | return config.matrix === 'Array' ? _ones([]) : _ones([], 'default');
|
60 | },
|
61 |
|
62 |
|
63 | '...number | BigNumber | string': function numberBigNumberString(size) {
|
64 | var last = size[size.length - 1];
|
65 |
|
66 | if (typeof last === 'string') {
|
67 | var format = size.pop();
|
68 | return _ones(size, format);
|
69 | } else if (config.matrix === 'Array') {
|
70 | return _ones(size);
|
71 | } else {
|
72 | return _ones(size, 'default');
|
73 | }
|
74 | },
|
75 | Array: _ones,
|
76 | Matrix: function Matrix(size) {
|
77 | var format = size.storage();
|
78 | return _ones(size.valueOf(), format);
|
79 | },
|
80 | 'Array | Matrix, string': function ArrayMatrixString(size, format) {
|
81 | return _ones(size.valueOf(), format);
|
82 | }
|
83 | });
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function _ones(size, format) {
|
93 | var hasBigNumbers = _normalize(size);
|
94 |
|
95 | var defaultValue = hasBigNumbers ? new BigNumber(1) : 1;
|
96 |
|
97 | _validate(size);
|
98 |
|
99 | if (format) {
|
100 |
|
101 | var m = matrix(format);
|
102 |
|
103 | if (size.length > 0) {
|
104 | return m.resize(size, defaultValue);
|
105 | }
|
106 |
|
107 | return m;
|
108 | } else {
|
109 |
|
110 | var arr = [];
|
111 |
|
112 | if (size.length > 0) {
|
113 | return (0, _array.resize)(arr, size, defaultValue);
|
114 | }
|
115 |
|
116 | return arr;
|
117 | }
|
118 | }
|
119 |
|
120 |
|
121 | function _normalize(size) {
|
122 | var hasBigNumbers = false;
|
123 | size.forEach(function (value, index, arr) {
|
124 | if ((0, _is.isBigNumber)(value)) {
|
125 | hasBigNumbers = true;
|
126 | arr[index] = value.toNumber();
|
127 | }
|
128 | });
|
129 | return hasBigNumbers;
|
130 | }
|
131 |
|
132 |
|
133 | function _validate(size) {
|
134 | size.forEach(function (value) {
|
135 | if (typeof value !== 'number' || !(0, _number.isInteger)(value) || value < 0) {
|
136 | throw new Error('Parameters in function ones must be positive integers');
|
137 | }
|
138 | });
|
139 | }
|
140 | });
|
141 | exports.createOnes = createOnes; |
\ | No newline at end of file |