Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions lib/node_modules/@stdlib/dstructs/named-typed-tuple/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,6 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
return out;
}

// TODO: consider adding `toLocaleString()` in a manner similar to `toString()` below

/**
* Serializes a tuple as a string.
*
Expand All @@ -1226,6 +1224,32 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
out += ')';
return out;
}
/**
* Serializes a tuple as a localized string.
*
* @private
* @memberof tuple
* @throws {TypeError} `this` must be the host tuple
* @returns {string} localized tuple string representation
*/
function toLocaleString() {
var out;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
out = opts.name + '(';
for ( i = 0; i < nfields; i++ ) {
out += fields[ i ];
out += '=';
out += tuple[ indices[ i ] ].toLocaleString();
if ( i < nfields-1 ) {
out += ', ';
}
}
out += ')';
return out;
}
}

// Note: keep the following methods in alphabetical order...
Expand Down Expand Up @@ -1397,11 +1421,26 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
return namedtypedtuple( args );
}
});

defineProperty( namedtypedtuple.prototype, 'toLocaleString', {
'configurable': false,
'enumerable': false,
'writable': false,
'value': function toLocaleString() {
return this.toString();
}
});
defineProperty( namedtypedtuple.prototype, 'toString', {
'configurable': false,
'enumerable': false,
'writable': false,
'value': toString
});


return namedtypedtuple;
}


// EXPORTS //

module.exports = factory;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var tape = require('tape');
var namedtypedtuple = require('./../lib/main.js');

tape('namedtypedtuple instances have a toLocaleString method', function test(t) {
var Person = namedtypedtuple(['age', 'salary'], { 'dtype': 'float64' });
var p = new Person([25, 1234567.89]);

t.equal(typeof p.toLocaleString, 'function', 'has method');

var str = p.toLocaleString();
t.equal(typeof str, 'string', 'returns a string');

t.end();
});