1616* limitations under the License.
1717*/
1818
19+ /* eslint-disable no-underscore-dangle */
20+
1921'use strict' ;
2022
2123// MODULES //
2224
2325var isComplexDataType = require ( '@stdlib/ndarray-base-assert-is-complex-floating-point-data-type' ) ;
24- var isString = require ( '@stdlib/assert-is-string' ) . isPrimitive ;
25- var isStringArray = require ( '@stdlib/assert-is-string-array' ) . primitives ;
2626var isUndefinedOrNull = require ( '@stdlib/assert-is-undefined-or-null' ) ;
27- var isObject = require ( '@stdlib/assert-is-plain-object' ) ;
2827var format = require ( '@stdlib/string-format' ) ;
2928var replace = require ( '@stdlib/string-replace' ) ;
3029var join = require ( '@stdlib/array-base-join' ) ;
@@ -52,76 +51,91 @@ var CTORS = {
5251} ;
5352
5453
54+ // FUNCTIONS //
55+
56+ /**
57+ * Serializes a value to a string.
58+ *
59+ * @private
60+ * @param {* } value - input value
61+ * @returns {string } output string
62+ */
63+ function toStr ( value ) {
64+ return String ( value ) ;
65+ }
66+
67+ /**
68+ * Serializes a value to a locale-aware string.
69+ *
70+ * @private
71+ * @param {* } value - input value
72+ * @param {(string|Array<string>) } [locales] - locale identifier(s)
73+ * @param {Object } [options] - configuration options
74+ * @returns {string } output string
75+ */
76+ function toLocaleStr ( value , locales , options ) {
77+ if ( ! isUndefinedOrNull ( value ) && value . toLocaleString ) {
78+ return value . toLocaleString ( locales , options ) ;
79+ }
80+ return toStr ( value ) ;
81+ }
82+
83+
5584// MAIN //
5685
5786/**
58- * Serializes an ndarray as a locale-aware string.
87+ * Serializes an ndarray to a string.
5988*
6089* ## Notes
6190*
6291* - The method does **not** serialize data outside of the buffer region defined by the array configuration.
6392*
6493* @private
65- * @param {(string|Array<string>) } [locales] - locale identifier(s)
66- * @param {Object } [options] - configuration options
67- * @throws { TypeError } first argument must be a string or an array of strings
68- * @throws { TypeError } options argument must be an object
94+ * @param {ndarray } ctx - ndarray object
95+ * @param {string } method - element serialization method (i.e., `toString` or `toLocaleString`)
96+ * @param { (string|Array<string>|null) } locales - locale identifier(s)
97+ * @param { (Object|null) } options - configuration options
6998* @returns {string } string representation
7099*/
71- function toLocaleString ( locales , options ) { // eslint-disable-line stdlib/no-redeclare
72- /* eslint-disable no-invalid-this */
100+ function serialize2string ( ctx , method , locales , options ) {
73101 var isCmplx ;
74102 var buffer ;
75103 var ndims ;
76104 var ctor ;
77- var opts ;
78- var loc ;
79105 var str ;
80106 var dt ;
107+ var f ;
81108 var v ;
82109 var i ;
83110
84- if ( arguments . length === 0 ) {
85- loc = [ ] ;
86- } else if ( isString ( locales ) || isStringArray ( locales ) ) {
87- loc = locales ;
111+ if ( method === 'toLocaleString' ) {
112+ f = toLocaleStr ;
88113 } else {
89- throw new TypeError ( format ( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.' , locales ) ) ;
114+ f = toStr ;
90115 }
91- if ( arguments . length < 2 ) {
92- opts = { } ;
93- } else if ( isObject ( options ) ) {
94- opts = options ;
95- } else {
96- throw new TypeError ( format ( 'invalid argument. Options argument must be an object. Value: `%s`.' , options ) ) ;
97- }
98- ndims = this . _shape . length ;
99- dt = this . _dtype ;
116+ ndims = ctx . _shape . length ;
117+ dt = ctx . _dtype ;
100118 isCmplx = isComplexDataType ( dt ) ;
101119
102120 // Function to invoke to create an ndarray:
103121 str = format ( 'ndarray( \'%s\', ' , String ( dt ) ) ;
104122
105123 // Data buffer parameter...
106124 buffer = '' ;
107- if ( this . _length <= 100 ) {
125+ if ( ctx . _length <= 100 ) {
108126 if ( isCmplx ) {
109- for ( i = 0 ; i < this . _length ; i ++ ) {
110- v = this . iget ( i ) ;
111- buffer += real ( v ) . toLocaleString ( loc , opts ) + ', ' + imag ( v ) . toLocaleString ( loc , opts ) ;
112- if ( i < this . _length - 1 ) {
127+ for ( i = 0 ; i < ctx . _length ; i ++ ) {
128+ v = ctx . iget ( i ) ;
129+ buffer += f ( real ( v ) , locales , options ) + ', ' + f ( imag ( v ) , locales , options ) ;
130+ if ( i < ctx . _length - 1 ) {
113131 buffer += ', ' ;
114132 }
115133 }
116134 } else {
117- for ( i = 0 ; i < this . _length ; i ++ ) {
118- v = this . iget ( i ) ;
119- if ( ! isUndefinedOrNull ( v ) && v . toLocaleString ) {
120- buffer += v . toLocaleString ( loc , opts ) ;
121- } else {
122- buffer += String ( v ) ;
123- }
124- if ( i < this . _length - 1 ) {
135+ for ( i = 0 ; i < ctx . _length ; i ++ ) {
136+ v = ctx . iget ( i ) ;
137+ buffer += f ( v , locales , options ) ;
138+ if ( i < ctx . _length - 1 ) {
125139 buffer += ', ' ;
126140 }
127141 }
@@ -130,20 +144,16 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
130144 // First three values...
131145 if ( isCmplx ) {
132146 for ( i = 0 ; i < 3 ; i ++ ) {
133- v = this . iget ( i ) ;
134- buffer += real ( v ) . toLocaleString ( loc , opts ) + ', ' + imag ( v ) . toLocaleString ( loc , opts ) ;
147+ v = ctx . iget ( i ) ;
148+ buffer += f ( real ( v ) , locales , options ) + ', ' + f ( imag ( v ) , locales , options ) ;
135149 if ( i < 2 ) {
136150 buffer += ', ' ;
137151 }
138152 }
139153 } else {
140154 for ( i = 0 ; i < 3 ; i ++ ) {
141- v = this . iget ( i ) ;
142- if ( ! isUndefinedOrNull ( v ) && v . toLocaleString ) {
143- buffer += v . toLocaleString ( loc , opts ) ;
144- } else {
145- buffer += String ( v ) ;
146- }
155+ v = ctx . iget ( i ) ;
156+ buffer += f ( v , locales , options ) ;
147157 if ( i < 2 ) {
148158 buffer += ', ' ;
149159 }
@@ -154,35 +164,31 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
154164 // Last three values...
155165 if ( isCmplx ) {
156166 for ( i = 2 ; i >= 0 ; i -- ) {
157- v = this . iget ( this . _length - 1 - i ) ;
158- buffer += real ( v ) . toLocaleString ( loc , opts ) + ', ' + imag ( v ) . toLocaleString ( loc , opts ) ;
167+ v = ctx . iget ( ctx . _length - 1 - i ) ;
168+ buffer += f ( real ( v ) , locales , options ) + ', ' + f ( imag ( v ) , locales , options ) ;
159169 if ( i > 0 ) {
160170 buffer += ', ' ;
161171 }
162172 }
163173 } else {
164174 for ( i = 2 ; i >= 0 ; i -- ) {
165- v = this . iget ( this . _length - 1 - i ) ;
166- if ( ! isUndefinedOrNull ( v ) && v . toLocaleString ) {
167- buffer += v . toLocaleString ( loc , opts ) ;
168- } else {
169- buffer += String ( v ) ;
170- }
175+ v = ctx . iget ( ctx . _length - 1 - i ) ;
176+ buffer += f ( v , locales , options ) ;
171177 if ( i > 0 ) {
172178 buffer += ', ' ;
173179 }
174180 }
175181 }
176182 }
177- ctor = CTORS [ this . dtype ] ;
183+ ctor = CTORS [ ctx . dtype ] ;
178184 str += replace ( ctor , '{{data}}' , buffer ) ;
179185 str += ', ' ;
180186
181187 // Array shape...
182188 if ( ndims === 0 ) {
183189 str += '[]' ;
184190 } else {
185- str += format ( '[ %s ]' , join ( this . _shape , ', ' ) ) ;
191+ str += format ( '[ %s ]' , join ( ctx . _shape , ', ' ) ) ;
186192 }
187193 str += ', ' ;
188194
@@ -192,10 +198,10 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
192198 str += '0' ;
193199 } else {
194200 for ( i = 0 ; i < ndims ; i ++ ) {
195- if ( this . _strides [ i ] < 0 ) {
196- str += - this . _strides [ i ] ;
201+ if ( ctx . _strides [ i ] < 0 ) {
202+ str += - ctx . _strides [ i ] ;
197203 } else {
198- str += this . _strides [ i ] ;
204+ str += ctx . _strides [ i ] ;
199205 }
200206 if ( i < ndims - 1 ) {
201207 str += ', ' ;
@@ -210,16 +216,14 @@ function toLocaleString( locales, options ) { // eslint-disable-line stdlib/no-r
210216 str += ', ' ;
211217
212218 // Order:
213- str += format ( '\'%s\'' , this . _order ) ;
219+ str += format ( '\'%s\'' , ctx . _order ) ;
214220
215221 // Close the function call:
216222 str += ' )' ;
217223 return str ;
218-
219- /* eslint-enable no-invalid-this */
220224}
221225
222226
223227// EXPORTS //
224228
225- module . exports = toLocaleString ;
229+ module . exports = serialize2string ;
0 commit comments