From cc153397f50a601b5d597ff13f8f2bcedf27e4ff Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 8 May 2025 08:52:33 +0000 Subject: [PATCH] Transform error messages --- lib/main.js | 32 ++++++++++++++++---------------- package.json | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/main.js b/lib/main.js index 8d596c5..88dcbce 100644 --- a/lib/main.js +++ b/lib/main.js @@ -46,7 +46,7 @@ var getOrder = require( '@stdlib/ndarray-order' ); var getData = require( '@stdlib/ndarray-data-buffer' ); var arrayShape = require( '@stdlib/array-shape' ); var flatten = require( '@stdlib/array-base-flatten' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var isArrayLikeObject = require( './is_array_like_object.js' ); var getDefaults = require( './defaults.js' ); var castBuffer = require( './cast_buffer.js' ); @@ -141,23 +141,23 @@ function array() { } else { options = arguments[ 0 ]; if ( !isObject( options ) ) { - throw new TypeError( format( 'invalid argument. Must provide either a valid data source, options argument, or both. Value: `%s`.', options ) ); + throw new TypeError( format( '0hT56', options ) ); } if ( hasOwnProp( options, 'buffer' ) ) { buffer = options.buffer; if ( !isArrayLikeObject( buffer ) ) { // weak test - throw new TypeError( format( 'invalid option. `%s` option must be an array-like object, typed-array-like, a Buffer, or an ndarray. Option: `%s`.', 'buffer', buffer ) ); + throw new TypeError( format( '0hT57', 'buffer', buffer ) ); } } } } else { buffer = arguments[ 0 ]; if ( !isArrayLikeObject( buffer ) ) { // weak test - throw new TypeError( format( 'invalid option. Data source must be an array-like object, typed-array-like, a Buffer, or an ndarray. Value: `%s`.', buffer ) ); + throw new TypeError( format( '0hT58', buffer ) ); } options = arguments[ 1 ]; if ( !isObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + throw new TypeError( format( '0hT2V', options ) ); } // Note: we ignore whether `options` has a `buffer` property } @@ -177,7 +177,7 @@ function array() { if ( hasOwnProp( options, 'casting' ) ) { opts.casting = options.casting; if ( !isCastingMode( opts.casting ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a recognized casting mode. Option: `%s`.', 'casting', opts.casting ) ); + throw new TypeError( format( '0hT59', 'casting', opts.casting ) ); } } else { opts.casting = defaults.casting; @@ -185,7 +185,7 @@ function array() { if ( hasOwnProp( options, 'flatten' ) ) { opts.flatten = options.flatten; if ( !isBoolean( opts.flatten ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'flatten', opts.flatten ) ); + throw new TypeError( format( '0hT2o', 'flatten', opts.flatten ) ); } } else { opts.flatten = defaults.flatten; @@ -193,7 +193,7 @@ function array() { if ( hasOwnProp( options, 'ndmin' ) ) { opts.ndmin = options.ndmin; if ( !isNonNegativeInteger( opts.ndmin ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'ndmin', opts.ndmin ) ); + throw new TypeError( format( '0hT2t', 'ndmin', opts.ndmin ) ); } // TODO: validate that minimum number of dimensions does not exceed the maximum number of possible dimensions (in theory, infinite; in practice, determined by max array length; see https://github.com/stdlib-js/stdlib/blob/ac350059877c036640775d6b30d0e98e840d07cf/lib/node_modules/%40stdlib/ndarray/ctor/lib/main.js#L57) } else { @@ -204,10 +204,10 @@ function array() { if ( hasOwnProp( options, 'dtype' ) ) { dtype = options.dtype; if ( !isDataType( dtype ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', dtype ) ); + throw new TypeError( format( '0hTBf', 'dtype', dtype ) ); } if ( btype && !isAllowedCast( btype, dtype, opts.casting ) ) { - throw new Error( format( 'invalid option. Data type cast is not allowed. Casting mode: `%s`. From: `%s`. To: `%s`.', opts.casting, btype, dtype ) ); + throw new Error( format( '0hT5B', opts.casting, btype, dtype ) ); } } else if ( btype ) { // btype !== void 0 // TODO: reconcile difference in behavior when provided a generic array and no `dtype` option. Currently, we cast here, but do not allow casting a generic array (by default) when explicitly providing a `dtype` option. @@ -245,7 +245,7 @@ function array() { order = defaults.order; } } else if ( !isOrder( order ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', order ) ); + throw new TypeError( format( '0hT5C', 'order', order ) ); } } else { order = defaults.order; @@ -268,7 +268,7 @@ function array() { if ( hasOwnProp( options, 'copy' ) ) { opts.copy = options.copy; if ( !isBoolean( opts.copy ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'copy', opts.copy ) ); + throw new TypeError( format( '0hT2o', 'copy', opts.copy ) ); } } else { opts.copy = defaults.copy; @@ -277,7 +277,7 @@ function array() { if ( hasOwnProp( options, 'shape' ) ) { shape = options.shape; if ( !isArrayLikeObject( shape ) ) { // weak test - throw new TypeError( format( 'invalid option. `%s` option must be an array-like object containing nonnegative integers. Option: `%s`.', 'shape', shape ) ); + throw new TypeError( format( '0hT5D', 'shape', shape ) ); } ndims = shape.length; len = numel( shape ); @@ -297,7 +297,7 @@ function array() { shape = [ len ]; // assume a 1-dimensional array (vector) } } else { - throw new Error( 'invalid arguments. Must provide either a data source, array shape, or both.' ); + throw new Error( format('0hT0X') ); } // Adjust the array shape to satisfy the minimum number of dimensions... if ( ndims < opts.ndmin ) { @@ -307,7 +307,7 @@ function array() { // If not provided a data buffer, create it; otherwise, see if we need to cast a provided data buffer to another data type or perform a copy... if ( FLG ) { if ( numel( buffer.shape ) !== len ) { - throw new RangeError( 'invalid arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' ); + throw new RangeError( format('0hT0Y') ); } if ( btype !== dtype || opts.copy ) { buffer = copyView( buffer, dtype ); @@ -325,7 +325,7 @@ function array() { buffer = flatten( buffer, osh || arrayShape( buffer ), isColumnMajor( order ) ); } if ( buffer.length !== len ) { - throw new RangeError( 'invalid arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' ); + throw new RangeError( format('0hT0Y') ); } if ( btype !== dtype || opts.copy ) { buffer = castBuffer( buffer, len, dtype ); diff --git a/package.json b/package.json index 44992cf..573f317 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,7 @@ "@stdlib/ndarray-order": "^0.2.2", "@stdlib/ndarray-shape": "^0.2.2", "@stdlib/ndarray-strides": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.3", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" },