Skip to content

Commit 43aae09

Browse files
committed
add toLocaleString method to namedtypedtuple with tests
1 parent 7b3badf commit 43aae09

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

lib/node_modules/@stdlib/dstructs/named-typed-tuple/lib/main.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,8 +1198,6 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
11981198
return out;
11991199
}
12001200

1201-
// Consider adding `toLocaleString()` in a manner similar to `toString()` below
1202-
12031201
/**
12041202
* Serializes a tuple as a string.
12051203
*
@@ -1226,6 +1224,32 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
12261224
out += ')';
12271225
return out;
12281226
}
1227+
/**
1228+
* Serializes a tuple as a localized string.
1229+
*
1230+
* @private
1231+
* @memberof tuple
1232+
* @throws {TypeError} `this` must be the host tuple
1233+
* @returns {string} localized tuple string representation
1234+
*/
1235+
function toLocaleString() {
1236+
var out;
1237+
var i;
1238+
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
1239+
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
1240+
}
1241+
out = opts.name + '(';
1242+
for ( i = 0; i < nfields; i++ ) {
1243+
out += fields[ i ];
1244+
out += '=';
1245+
out += tuple[ indices[ i ] ].toLocaleString();
1246+
if ( i < nfields-1 ) {
1247+
out += ', ';
1248+
}
1249+
}
1250+
out += ')';
1251+
return out;
1252+
}
12291253
}
12301254

12311255
// Note: keep the following methods in alphabetical order...
@@ -1397,11 +1421,26 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
13971421
return namedtypedtuple( args );
13981422
}
13991423
});
1424+
1425+
defineProperty( namedtypedtuple.prototype, 'toLocaleString', {
1426+
'configurable': false,
1427+
'enumerable': false,
1428+
'writable': false,
1429+
'value': function toLocaleString() {
1430+
return this.toString();
1431+
}
1432+
});
1433+
defineProperty( namedtypedtuple.prototype, 'toString', {
1434+
'configurable': false,
1435+
'enumerable': false,
1436+
'writable': false,
1437+
'value': toString
1438+
});
14001439

1440+
14011441
return namedtypedtuple;
14021442
}
14031443

1404-
14051444
// EXPORTS //
14061445

14071446
module.exports = factory;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var tape = require('tape');
4+
var namedtypedtuple = require('./../lib/main.js');
5+
6+
tape('namedtypedtuple instances have a toLocaleString method', function test(t) {
7+
var Person = namedtypedtuple(['age', 'salary'], { 'dtype': 'float64' });
8+
var p = new Person([25, 1234567.89]);
9+
10+
t.equal(typeof p.toLocaleString, 'function', 'has method');
11+
12+
var str = p.toLocaleString();
13+
t.equal(typeof str, 'string', 'returns a string');
14+
15+
t.end();
16+
});

0 commit comments

Comments
 (0)