🚨 Active Supply Chain Attack:node-ipc Package Compromised.Learn More
Socket
Book a DemoSign in
Socket

@stdlib/array-base-accessor-getter

Package Overview
Dependencies
Maintainers
4
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stdlib/array-base-accessor-getter - npm Package Compare versions

Comparing version
0.0.1
to
0.1.0
CITATION.cff

Sorry, the diff of this file is not supported yet

+3
/// <reference path="../docs/types/index.d.ts" />
import getter from '../docs/types/index';
export = getter;
"use strict";var o=function(t,e){return function(){return e||t((e={exports:{}}).exports,e),e.exports}};var u=o(function(l,n){
var r={complex128:i,complex64:f,default:c};function i(t,e){return t.get(e)}function f(t,e){return t.get(e)}function c(t,e){return t.get(e)}function a(t){var e=r[t];return typeof e=="function"?e:r.default}n.exports=a
});var g=u();module.exports=g;
/** @license Apache-2.0 */
//# sourceMappingURL=index.js.map
{
"version": 3,
"sources": ["../lib/main.js", "../lib/index.js"],
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// VARIABLES //\n\nvar GETTERS = {\n\t'complex128': getComplex128,\n\t'complex64': getComplex64,\n\t'default': getArrayLike\n};\n\n\n// FUNCTIONS //\n\n/**\n* Returns an element from a `Complex128Array`.\n*\n* @private\n* @param {Complex128Array} arr - input array\n* @param {NonNegativeInteger} idx - element index\n* @returns {number} element value\n*\n* @example\n* var Complex128Array = require( '@stdlib/array-complex128' );\n* var real = require( '@stdlib/complex-real' );\n* var imag = require( '@stdlib/complex-imag' );\n*\n* var arr = new Complex128Array( [ 1, 2, 3, 4 ] );\n*\n* var v = getComplex128( arr, 1 );\n* // returns <Complex128>\n*\n* var re = real( v );\n* // returns 3.0\n*\n* var im = imag( v );\n* // returns 4.0\n*/\nfunction getComplex128( arr, idx ) {\n\treturn arr.get( idx );\n}\n\n/**\n* Returns an element from a `Complex64Array`.\n*\n* @private\n* @param {Complex64Array} arr - input array\n* @param {NonNegativeInteger} idx - element index\n* @returns {number} element value\n*\n* @example\n* var Complex64Array = require( '@stdlib/array-complex64' );\n* var realf = require( '@stdlib/complex-realf' );\n* var imagf = require( '@stdlib/complex-imagf' );\n*\n* var arr = new Complex64Array( [ 1, 2, 3, 4 ] );\n*\n* var v = getComplex64( arr, 1 );\n* // returns <Complex64>\n*\n* var re = realf( v );\n* // returns 3.0\n*\n* var im = imagf( v );\n* // returns 4.0\n*/\nfunction getComplex64( arr, idx ) {\n\treturn arr.get( idx );\n}\n\n/**\n* Returns an element from an array-like object supporting the get/set protocol.\n*\n* @private\n* @param {Collection} arr - input array\n* @param {NonNegativeInteger} idx - element index\n* @returns {*} element value\n*\n* @example\n* var arr = [ 1, 2, 3, 4 ];\n*\n* function get( idx ) {\n* return arr[ idx ];\n* }\n*\n* function set( value, idx ) {\n* arr[ idx ] = value;\n* }\n*\n* arr.get = get;\n* arr.set = set;\n*\n* var v = getArrayLike( arr, 2 );\n* // returns 3\n*/\nfunction getArrayLike( arr, idx ) {\n\treturn arr.get( idx );\n}\n\n\n// MAIN //\n\n/**\n* Returns an accessor function for retrieving an element from an array-like object supporting the get/set protocol.\n*\n* @param {string} dtype - array dtype\n* @returns {Function} accessor\n*\n* @example\n* var Complex64Array = require( '@stdlib/array-complex64' );\n* var realf = require( '@stdlib/complex-realf' );\n* var imagf = require( '@stdlib/complex-imagf' );\n* var dtype = require( '@stdlib/array-dtype' );\n*\n* var arr = new Complex64Array( [ 1, 2, 3, 4 ] );\n*\n* var get = getter( dtype( arr ) );\n* var v = get( arr, 1 );\n* // returns <Complex64>\n*\n* var re = realf( v );\n* // returns 3.0\n*\n* var im = imagf( v );\n* // returns 4.0\n*/\nfunction getter( dtype ) {\n\tvar f = GETTERS[ dtype ];\n\tif ( typeof f === 'function' ) {\n\t\treturn f;\n\t}\n\treturn GETTERS.default;\n}\n\n\n// EXPORTS //\n\nmodule.exports = getter;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Return an accessor function for retrieving an element from an array-like object supporting the get/set protocol.\n*\n* @module @stdlib/array-base-accessor-getter\n*\n* @example\n* var Complex64Array = require( '@stdlib/array-complex64' );\n* var realf = require( '@stdlib/complex-realf' );\n* var imagf = require( '@stdlib/complex-imagf' );\n* var dtype = require( '@stdlib/array-dtype' );\n* var get = require( '@stdlib/array-base-accessor-getter' );\n*\n* var arr = new Complex64Array( [ 1, 2, 3, 4 ] );\n*\n* var get = getter( dtype( arr ) );\n* var v = get( arr, 1 );\n* // returns <Complex64>\n*\n* var re = realf( v );\n* // returns 3.0\n*\n* var im = imagf( v );\n* // returns 4.0\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,CACb,WAAcC,EACd,UAAaC,EACb,QAAWC,CACZ,EA6BA,SAASF,EAAeG,EAAKC,EAAM,CAClC,OAAOD,EAAI,IAAKC,CAAI,CACrB,CA0BA,SAASH,EAAcE,EAAKC,EAAM,CACjC,OAAOD,EAAI,IAAKC,CAAI,CACrB,CA2BA,SAASF,EAAcC,EAAKC,EAAM,CACjC,OAAOD,EAAI,IAAKC,CAAI,CACrB,CA6BA,SAASC,EAAQC,EAAQ,CACxB,IAAIC,EAAIR,EAASO,CAAM,EACvB,OAAK,OAAOC,GAAM,WACVA,EAEDR,EAAQ,OAChB,CAKAD,EAAO,QAAUO,IC3GjB,IAAIG,EAAO,IAKX,OAAO,QAAUA",
"names": ["require_main", "__commonJSMin", "exports", "module", "GETTERS", "getComplex128", "getComplex64", "getArrayLike", "arr", "idx", "getter", "dtype", "f", "main"]
}
+4
-4

@@ -19,7 +19,7 @@ /*

// TypeScript Version: 2.0
// TypeScript Version: 4.1
/// <reference types="@stdlib/types"/>
import { Complex64, Complex128 } from '@stdlib/types/object';
import { Complex64, Complex128 } from '@stdlib/types/complex';
import { Complex64Array, Complex128Array, AccessorArrayLike } from '@stdlib/types/array';

@@ -53,3 +53,3 @@

*/
type GetArrayLike = ( arr: AccessorArrayLike<any>, idx: number ) => any;
type GetArrayLike<T> = ( arr: AccessorArrayLike<T>, idx: number ) => T;

@@ -130,3 +130,3 @@ /**

*/
declare function getter( dtype: string ): GetArrayLike;
declare function getter<T = unknown>( dtype: string ): GetArrayLike<T>; // tslint:disable-line:no-unnecessary-generics

@@ -133,0 +133,0 @@

{
"name": "@stdlib/array-base-accessor-getter",
"version": "0.0.1",
"version": "0.1.0",
"description": "Return an accessor function for retrieving an element from an array-like object supporting the get/set protocol.",

@@ -40,19 +40,19 @@ "license": "Apache-2.0",

"dependencies": {
"@stdlib/types": "^0.0.x"
"@stdlib/types": "^0.1.0"
},
"devDependencies": {
"@stdlib/array-base-zero-to": "^0.0.x",
"@stdlib/array-complex128": "^0.0.x",
"@stdlib/array-complex64": "^0.0.x",
"@stdlib/array-dtype": "^0.0.x",
"@stdlib/array-filled-by": "^0.0.x",
"@stdlib/assert-is-function": "^0.0.x",
"@stdlib/bench": "^0.0.x",
"@stdlib/complex-imag": "^0.0.x",
"@stdlib/complex-imagf": "^0.0.x",
"@stdlib/complex-real": "^0.0.x",
"@stdlib/complex-realf": "^0.0.x",
"@stdlib/math-base-assert-is-nan": "^0.0.x",
"@stdlib/math-base-assert-is-nanf": "^0.0.x",
"@stdlib/random-base-discrete-uniform": "^0.0.x",
"@stdlib/array-base-zero-to": "^0.1.0",
"@stdlib/array-complex128": "^0.0.6",
"@stdlib/array-complex64": "^0.0.6",
"@stdlib/array-dtype": "^0.0.6",
"@stdlib/array-filled-by": "^0.0.2",
"@stdlib/assert-is-function": "^0.1.0",
"@stdlib/bench": "^0.1.0",
"@stdlib/complex-imag": "^0.1.0",
"@stdlib/complex-imagf": "^0.1.0",
"@stdlib/complex-real": "^0.1.0",
"@stdlib/complex-realf": "^0.1.0",
"@stdlib/math-base-assert-is-nan": "^0.1.0",
"@stdlib/math-base-assert-is-nanf": "^0.1.0",
"@stdlib/random-base-discrete-uniform": "^0.0.6",
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",

@@ -88,5 +88,5 @@ "istanbul": "^0.4.1",

"funding": {
"type": "patreon",
"url": "https://www.patreon.com/athan"
"type": "opencollective",
"url": "https://opencollective.com/stdlib"
}
}

@@ -21,2 +21,13 @@ <!--

<details>
<summary>
About stdlib...
</summary>
<p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
<p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
<p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
<p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
</details>
# accessorGetter

@@ -192,4 +203,4 @@

[test-image]: https://github.com/stdlib-js/array-base-accessor-getter/actions/workflows/test.yml/badge.svg?branch=v0.0.1
[test-url]: https://github.com/stdlib-js/array-base-accessor-getter/actions/workflows/test.yml?query=branch:v0.0.1
[test-image]: https://github.com/stdlib-js/array-base-accessor-getter/actions/workflows/test.yml/badge.svg?branch=v0.1.0
[test-url]: https://github.com/stdlib-js/array-base-accessor-getter/actions/workflows/test.yml?query=branch:v0.1.0

@@ -207,3 +218,3 @@ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-base-accessor-getter/main.svg

[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
[chat-url]: https://gitter.im/stdlib-js/stdlib/
[chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im

@@ -210,0 +221,0 @@ [stdlib]: https://github.com/stdlib-js/stdlib

{{alias}}( dtype )
Returns an accessor function for retrieving an element from an array-like
object supporting the get/set protocol.
An accessor function accepts the following arguments:
- arr: input array
- idx: element index
If provided an unsupported `dtype`, the function returns a default accessor
function for accessing elements from any indexed array-like object
supporting the get/set protocol.
Otherwise, the function returns an accessor function which should *only* be
provided an array instance corresponding to `dtype` (e.g., if `dtype` is
'complex64', the returned accessor function should only be provided
instances of Complex64Array).
Accessor functions do *not* verify that provided input arrays are array
instances corresponding to `dtype`, as doing so would introduce performance
overhead. If array instances corresponding to other data types are provided
to an accessor function, JavaScript runtimes will consider the function
polymorphic, potentially triggering de-optimization. In order to ensure
maximum performance, *always* ensure that an accessor function is
monomorphic.
Accessor functions do *not* perform bounds checking.
Accessor functions do *not* verify that provided input arrays actually
implement the get/set protocol.
Parameters
----------
dtype: string
Array data type.
Returns
-------
f: Function
Accessor function.
Examples
--------
> var f = {{alias}}( 'complex64' );
> var v = f( {{alias:@stdlib/array/complex64}}( [ 1, 2, 3, 4 ] ), 1 )
<Complex64>
> var r = {{alias:@stdlib/complex/realf}}( v )
3.0
> var i = {{alias:@stdlib/complex/imagf}}( v )
4.0
See Also
--------
/*
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference types="@stdlib/types"/>
import { AccessorArrayLike } from '@stdlib/types/array';
import Complex128Array = require( '@stdlib/array-complex128' );
import Complex64Array = require( '@stdlib/array-complex64' );
import getter = require( './index' );
/**
* Returns an array-like object supporting the get/set protocol.
*
* @return array-like object
*/
function accessorArray(): AccessorArrayLike<number> {
let arr: AccessorArrayLike<number>;
arr = {
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'length': 4,
'get': ( idx: number ): number => {
return arr[ idx ]; // tslint:disable-line:no-unsafe-any
},
'set': ( value: number, idx: number ): void => {
arr[ idx ] = value;
}
};
return arr;
}
// TESTS //
// The function returns a function...
{
getter( 'complex128' ); // $ExpectType GetComplex128
getter( 'complex64' ); // $ExpectType GetComplex64
getter( 'foo' ); // $ExpectType GetArrayLike
}
// The compiler throws an error if the function is provided a first argument which is not a string...
{
getter( 5 ); // $ExpectError
getter( true ); // $ExpectError
getter( false ); // $ExpectError
getter( null ); // $ExpectError
getter( {} ); // $ExpectError
getter( [] ); // $ExpectError
getter( ( x: number ): number => x ); // $ExpectError
}
// The compiler throws an error if the function is provided an unsupported number of arguments...
{
getter(); // $ExpectError
getter( 'complex128', {} ); // $ExpectError
}
// The function returns a function which returns an array element...
{
const get1 = getter( 'foo' );
const x1 = accessorArray();
get1( x1, 2 ); // $ExpectType any
const get2 = getter( 'complex128' );
const x2 = new Complex128Array( [ 1, 2, 3, 4 ] );
get2( x2, 1 ); // $ExpectType void | Complex128
const get3 = getter( 'complex64' );
const x3 = new Complex64Array( [ 1, 2, 3, 4 ] );
get3( x3, 1 ); // $ExpectType void | Complex64
}
// The compiler throws an error if the returned function is provided a first argument which is not a accessor array...
{
const get1 = getter( 'foo' );
get1( 5, 1 ); // $ExpectError
get1( true, 1 ); // $ExpectError
get1( false, 1 ); // $ExpectError
get1( null, 1 ); // $ExpectError
get1( {}, 1 ); // $ExpectError
const get2 = getter( 'complex128' );
get2( 5, 1 ); // $ExpectError
get2( true, 1 ); // $ExpectError
get2( false, 1 ); // $ExpectError
get2( null, 1 ); // $ExpectError
get2( {}, 1 ); // $ExpectError
const get3 = getter( 'complex64' );
get3( 5, 1 ); // $ExpectError
get3( true, 1 ); // $ExpectError
get3( false, 1 ); // $ExpectError
get3( null, 1 ); // $ExpectError
get3( {}, 1 ); // $ExpectError
}
// The compiler throws an error if the returned function is provided a second argument which is not a number...
{
const get1 = getter( 'foo' );
get1( accessorArray(), '5' ); // $ExpectError
get1( accessorArray(), true ); // $ExpectError
get1( accessorArray(), false ); // $ExpectError
get1( accessorArray(), null ); // $ExpectError
get1( accessorArray(), {} ); // $ExpectError
const get2 = getter( 'complex128' );
get2( new Complex128Array( [ 1, 2, 3, 4 ] ), '5' ); // $ExpectError
get2( new Complex128Array( [ 1, 2, 3, 4 ] ), true ); // $ExpectError
get2( new Complex128Array( [ 1, 2, 3, 4 ] ), false ); // $ExpectError
get2( new Complex128Array( [ 1, 2, 3, 4 ] ), null ); // $ExpectError
get2( new Complex128Array( [ 1, 2, 3, 4 ] ), {} ); // $ExpectError
const get3 = getter( 'complex64' );
get3( new Complex64Array( [ 1, 2, 3, 4 ] ), '5' ); // $ExpectError
get3( new Complex64Array( [ 1, 2, 3, 4 ] ), true ); // $ExpectError
get3( new Complex64Array( [ 1, 2, 3, 4 ] ), false ); // $ExpectError
get3( new Complex64Array( [ 1, 2, 3, 4 ] ), null ); // $ExpectError
get3( new Complex64Array( [ 1, 2, 3, 4 ] ), {} ); // $ExpectError
}
// The compiler throws an error if the returned function is provided an unsupported number of arguments...
{
const get1 = getter( 'foo' );
get1(); // $ExpectError
get1( [] ); // $ExpectError
get1( [], 1, 2 ); // $ExpectError
const get2 = getter( 'complex128' );
get2(); // $ExpectError
get2( new Complex128Array( [] ) ); // $ExpectError
get2( new Complex128Array( [] ), 1, 2 ); // $ExpectError
const get3 = getter( 'complex64' );
get3(); // $ExpectError
get3( new Complex64Array( [] ) ); // $ExpectError
get3( new Complex64Array( [] ), 1, 2 ); // $ExpectError
}