Skip to content

Commit fd9a4f3

Browse files
feat(named-typed-tuple): implement toLocaleString function
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent fe96d97 commit fd9a4f3

File tree

2 files changed

+31
-1
lines changed
  • lib/node_modules/@stdlib/dstructs/named-typed-tuple

2 files changed

+31
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ console.log( p.orderedFields );
5757
// Serialize the tuple as a string:
5858
console.log( p.toString() );
5959

60+
// Serialize the tuple as a locale string
61+
console.log( p.toLocaleString() );
62+
6063
// Serialize the tuple a JSON string:
6164
console.log( JSON.stringify( p ) );

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
204204
setNonEnumerableProperty( tuple, 'sort', sort );
205205
setNonEnumerableProperty( tuple, 'subtuple', subtuple );
206206
setNonEnumerableProperty( tuple, 'toJSON', toJSON );
207+
setNonEnumerableProperty( tuple, 'toLocaleString', toLocaleString );
207208
setNonEnumerableProperty( tuple, 'toString', toString );
208209

209210
return tuple;
@@ -1198,7 +1199,33 @@ function factory( names, options ) { // eslint-disable-line max-lines-per-functi
11981199
return out;
11991200
}
12001201

1201-
// TODO: consider adding `toLocaleString()` in a manner similar to `toString()` below
1202+
/**
1203+
* Serializes a tuple as a locale-aware string.
1204+
*
1205+
* @private
1206+
* @memberof tuple
1207+
* @param {(string|string[])} [locales] - locale(s)
1208+
* @param {Object} [options] - formatting options
1209+
* @throws {TypeError} `this` must be the host tuple
1210+
* @returns {string} tuple locale string representation
1211+
*/
1212+
function toLocaleString( locales, options ) {
1213+
var out = [];
1214+
var val;
1215+
var i;
1216+
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
1217+
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
1218+
}
1219+
for ( i = 0; i < nfields; i++ ) {
1220+
val = tuple[ indices[ i ] ];
1221+
if ( val && typeof val.toLocaleString === 'function' ) {
1222+
out.push( val.toLocaleString( locales, options ) );
1223+
} else {
1224+
out.push( String( val ) );
1225+
}
1226+
}
1227+
return out.join(',');
1228+
}
12021229

12031230
/**
12041231
* Serializes a tuple as a string.

0 commit comments

Comments
 (0)