@stdlib/array-index

Array index constructor.

https://github.com/stdlib-js/array-index

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.0%) to scientific vocabulary

Keywords

array constructor ctor data fancy index indexing javascript node node-js nodejs slice stdlib structure types vector
Last synced: 6 months ago · JSON representation ·

Repository

Array index constructor.

Basic Info
Statistics
  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
array constructor ctor data fancy index indexing javascript node node-js nodejs slice stdlib structure types vector
Created about 2 years ago · Last pushed 6 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security

README.md

About stdlib...

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.

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.

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.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

ArrayIndex

NPM version Build Status Coverage Status <!-- dependencies -->

Array index constructor.

In JavaScript, only strings and symbols are valid property names. When providing values for property names which are not strings or symbols, the values are serialized to strings **prior to** attempting to access property values. For example, the following ```javascript // Create an array: var x = [ 1, 2, 3, 4 ]; // Define a list of indices for elements we want to retrieve from `x`: var y = [ 0, 2 ]; // Attempt to retrieve the desired elements: var v = x[ y ]; // => desired: [ 1, 3 ] // returns undefined ``` is equivalent to ```javascript var x = [ 1, 2, 3, 4 ]; var y = [ 0, 2 ]; var v = x[ y.toString() ]; // returns undefined // ...which is equivalent to: v = x[ '0,2' ]; // returns undefined ``` Accordingly, in order to circumvent built-in property access behavior and support non-traditional access patterns, one can leverage [`Proxy`][@stdlib/proxy/ctor] objects which allow one to intercept property access and to perform transformations before attempting to access elements in a target object. To support the access pattern shown in the example above, one can leverage built-in string serialization behavior to reconstruct the original property value provided prior to serialization. The `ArrayIndex` constructor described below provides one such mechanism. Specifically, instantiated `ArrayIndex` objects are assigned a unique identifier and stored in a local cache. When provided as property values to `ArrayIndex` consumers, instantiated objects serialize to a string containing their unique identifier. `ArrayIndex` consumers can then parse the serialized string to obtain the unique identifier and subsequently recover the original array from the local cache.
## Installation ```bash npm install @stdlib/array-index ``` Alternatively, - To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]). - If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions). - For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]). The [branches.md][branches-url] file summarizes the available branches and displays a diagram illustrating their relationships. To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
## Usage ```javascript var ArrayIndex = require( '@stdlib/array-index' ); ``` #### ArrayIndex( x\[, options] ) Wraps a provided array as an array index object. ```javascript var x = [ 1, 2, 3, 4 ]; var idx = new ArrayIndex( x ); // returns ``` The constructor accepts the following arguments: - **x**: input array. - **options**: function options. The constructor accepts the following options: - **persist**: boolean indicating whether to continue persisting an index object after first usage. Default: `false`. By default, an `ArrayIndex` is invalidated and removed from an internal cache immediately after a consumer resolves the underlying data associated with an `ArrayIndex` instance using the [`ArrayIndex.get()`](#static-method-get) static method. Immediate invalidation and cache removal ensures that references to the underlying array are not the source of memory leaks. One may, however, want to reuse an `ArrayIndex` instance to avoid additional memory allocation. In order to persist an `ArrayIndex` and prevent automatic cache invalidation, set the `persist` option to `true`. ```javascript var x = [ 1, 2, 3, 4 ]; var idx = new ArrayIndex( x, { 'persist': true }); // returns // ... var o = ArrayIndex.get( idx.id ); // returns {...} // ... o = ArrayIndex.get( idx.id ); // returns {...} // ... // Explicitly free the array index: ArrayIndex.free( idx.id ); ``` In order to **prevent** memory leaks when working with persisted `ArrayIndex` instances, one **must** remember to manually free persisted instances using the [`ArrayIndex.free()`](#static-method-free) method. * * * ### Properties #### ArrayIndex.name String value of the `ArrayIndex` constructor name. ```javascript var str = ArrayIndex.name; // returns 'ArrayIndex' ``` #### ArrayIndex.prototype.data **Read-only** property returning the underlying array associated with an `ArrayIndex` instance. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) ); // returns var v = idx.data; // returns [ 1, 0, 1, 0 ] ``` #### ArrayIndex.prototype.dtype **Read-only** property returning the data type of the underlying array associated with an `ArrayIndex` instance. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) ); // returns var dt = idx.dtype; // returns 'uint8' ``` #### ArrayIndex.prototype.id **Read-only** property returning the unique identifier associated with an `ArrayIndex` instance. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) ); // returns var id = idx.id; // returns ``` The identifier should be used by `ArrayIndex` consumers to resolve the underlying data associated with an `ArrayIndex` instance. #### ArrayIndex.prototype.isCached **Read-only** property returning a boolean indicating whether an `ArrayIndex` instance is actively cached. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) ); // returns var out = idx.isCached; // returns true ``` #### ArrayIndex.prototype.type **Read-only** property returning the array index type. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) ); // returns var t = idx.type; // returns 'mask' ``` The following array index types are supported: - **mask**: mask array, in which a value of zero indicates to include a respective element and a value of one indicates to exclude a respective element. A mask array is the complement of a boolean array. - **bool**: boolean array, in which a value of `true` indicates to include a respective element and a value of `false` indicates to exclude a respective element. A boolean array is the complement of a mask array. - **int**: integer array, in which each element is an index indicating the position of an element to include. Elements are **not** required to be unique (i.e., more than element may resolve to the same position). * * * ### Methods #### ArrayIndex.free( id ) Frees the `ArrayIndex` associated with a provided identifier. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), { 'persist': true }); // returns // ... var out = ArrayIndex.free( idx.id ); // returns true ``` Once an `ArrayIndex` is freed, the instance is invalid and can no longer be used. Any subsequent `ArrayIndex` operations (i.e., property and method access) will raise an exception. #### ArrayIndex.get( id ) Returns the array associated with the `ArrayIndex` having a provided identifier. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), { 'persist': true }); // returns // ... var o = ArrayIndex.get( idx.id ); // returns {...} var d = o.data; // returns [ 1, 0, 1, 0 ] var t = o.type; // returns 'mask' var dt = o.dtype; // returns 'uint8' ``` The returned object has the following properties: - **data**: the underlying array associated with the `ArrayIndex` identified by the provided `id`. - **type**: the type of array index. One of the following: `'int'`, `'bool'`, or `'mask'`. - **dtype**: the data type of the underlying array. If the `ArrayIndex` associated with a provided identifier was not explicitly persisted, calling this method will cause the `ArrayIndex` to be invalidated and removed from an internal cache. Any subsequent instance operations (i.e., property and method access) will raise an exception. #### ArrayIndex.prototype.toString() Serializes an `ArrayIndex` as a string. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) ); // returns var str = idx.toString(); // e.g., 'ArrayIndex<0>' ``` An `ArrayIndex` is intended to be an opaque object used by objects supporting "fancy" indexing (e.g., [fancy arrays][@stdlib/array/to-fancy]). As such, when serialized as a string, a serialized `ArrayIndex` includes only the unique identifier associated with the respective instance. #### ArrayIndex.prototype.toJSON() Serializes an `ArrayIndex` as a [JSON][json] object. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) ); // returns var o = idx.toJSON(); // returns { 'type': 'ArrayIndex', 'data': { 'type': 'Uint8Array', 'data': [ 1, 0, 1, 0 ] } } ``` `JSON.stringify()` implicitly calls this method when stringifying an `ArrayIndex` instance.

## Notes - `ArrayIndex` instances have no explicit functionality; however, they are used by ["fancy" arrays][@stdlib/array/to-fancy] and other packages for element retrieval and assignment. - Because `ArrayIndex` instances leverage an internal cache implementing the **singleton pattern**, one **must** be sure to use the same `ArrayIndex` constructor as `ArrayIndex` consumers. If one uses a different `ArrayIndex` constructor, the consumer will **not** be able to resolve the original wrapped array, as the consumer will attempt to resolve an `ArrayIndex` instance in the wrong internal cache. - Because non-persisted `ArrayIndex` instances are freed after first use, in order to avoid holding onto memory and to allow garbage collection, one should avoid scenarios in which an `ArrayIndex` is never used. For example, ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var data = new Uint8Array( [ 1, 0, 0, 0 ] ); var idx = new ArrayIndex( data ); var o; if ( data[ 0 ] === 0 ) { // Do something with `idx`... o = ArrayIndex.get( idx.id ); // ... } ``` will leak memory as `idx` is only consumed within an `if` block which never evaluates. In such scenarios, one should either refactor to avoid inadvertently holding onto memory or explicitly free the `ArrayIndex`. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var data = new Uint8Array( [ 1, 0, 0, 0 ] ); var idx = new ArrayIndex( data ); var o; if ( data[ 0 ] === 0 ) { // Do something with `idx`... o = ArrayIndex.get( idx.id ); // ... } else { ArrayIndex.free( idx.id ); } ```

## Examples ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var Int32Array = require( '@stdlib/array-int32' ); var BooleanArray = require( '@stdlib/array-bool' ); var ArrayIndex = require( '@stdlib/array-index' ); var x = new Uint8Array( [ 1, 0, 1, 0 ] ); var i = new ArrayIndex( x ); // returns var o = ArrayIndex.get( i.id ); // returns {...} console.log( 'Type: %s. Data type: %s.', o.type, o.dtype ); x = [ true, false, true, false ]; i = new ArrayIndex( x ); // returns o = ArrayIndex.get( i.id ); // returns {...} console.log( 'Type: %s. Data type: %s.', o.type, o.dtype ); x = new BooleanArray( [ true, false, true, false ] ); i = new ArrayIndex( x ); // returns o = ArrayIndex.get( i.id ); // returns {...} console.log( 'Type: %s. Data type: %s.', o.type, o.dtype ); x = new Int32Array( [ 1, 3, 4, 7 ] ); i = new ArrayIndex( x ); // returns o = ArrayIndex.get( i.id ); // returns {...} console.log( 'Type: %s. Data type: %s.', o.type, o.dtype ); x = [ 1, 3, 4, 7 ]; i = new ArrayIndex( x ); // returns o = ArrayIndex.get( i.id ); // returns {...} console.log( 'Type: %s. Data type: %s.', o.type, o.dtype ); ```
* * * ## Notice This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. #### Community [![Chat][chat-image]][chat-url] --- ## License See [LICENSE][stdlib-license]. ## Copyright Copyright © 2016-2025. The Stdlib [Authors][stdlib-authors].

Owner

  • Name: stdlib
  • Login: stdlib-js
  • Kind: organization

Standard library for JavaScript.

Citation (CITATION.cff)

cff-version: 1.2.0
title: stdlib
message: >-
  If you use this software, please cite it using the
  metadata from this file.

type: software

authors:
  - name: The Stdlib Authors
    url: https://github.com/stdlib-js/stdlib/graphs/contributors

repository-code: https://github.com/stdlib-js/stdlib
url: https://stdlib.io

abstract: |
  Standard library for JavaScript and Node.js.

keywords:
  - JavaScript
  - Node.js
  - TypeScript
  - standard library
  - scientific computing
  - numerical computing
  - statistical computing

license: Apache-2.0 AND BSL-1.0

date-released: 2016

GitHub Events

Total
  • Push event: 30
Last Year
  • Push event: 30

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 22
  • Total Committers: 1
  • Avg Commits per committer: 22.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 9
  • Committers: 1
  • Avg Commits per committer: 9.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
stdlib-bot n****y@s****o 22
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • npm 56 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 4
  • Total maintainers: 4
npmjs.org: @stdlib/array-index

Array index constructor.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.3.0
    published over 1 year ago
  • Versions: 4
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 56 Last month
Rankings
Dependent repos count: 33.9%
Average: 41.2%
Dependent packages count: 48.5%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 6 months ago

Dependencies

.github/workflows/benchmark.yml actions
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/cancel.yml actions
  • styfle/cancel-workflow-action 85880fa0301c86cca9da44039ee3bb12d3bedbfa composite
.github/workflows/close_pull_requests.yml actions
  • superbrothers/close-pull-request 9c18513d320d7b2c7185fb93396d0c664d5d8448 composite
.github/workflows/examples.yml actions
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/npm_downloads.yml actions
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • actions/upload-artifact a8a3f3ad30e3422c9c7b888a15615d19a852ae32 composite
  • distributhor/workflow-webhook 48a40b380ce4593b6a6676528cd005986ae56629 composite
.github/workflows/productionize.yml actions
  • act10ns/slack ed1309ab9862e57e9e583e51c7889486b9a00b0f composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • stdlib-js/bundle-action main composite
  • stdlib-js/transform-errors-action main composite
.github/workflows/publish.yml actions
  • JS-DevTools/npm-publish 4b07b26a2f6e0a51846e1870223e545bae91c552 composite
  • act10ns/slack ed1309ab9862e57e9e583e51c7889486b9a00b0f composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • styfle/cancel-workflow-action 85880fa0301c86cca9da44039ee3bb12d3bedbfa composite
.github/workflows/test.yml actions
  • act10ns/slack ed1309ab9862e57e9e583e51c7889486b9a00b0f composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/test_bundles.yml actions
  • act10ns/slack ed1309ab9862e57e9e583e51c7889486b9a00b0f composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • denoland/setup-deno 61fe2df320078202e33d7d5ad347e7dcfa0e8f31 composite
.github/workflows/test_coverage.yml actions
  • act10ns/slack ed1309ab9862e57e9e583e51c7889486b9a00b0f composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • codecov/codecov-action eaaf4bedf32dbdc6b720b63067d99c4d77d6047d composite
  • distributhor/workflow-webhook 48a40b380ce4593b6a6676528cd005986ae56629 composite
.github/workflows/test_install.yml actions
  • act10ns/slack ed1309ab9862e57e9e583e51c7889486b9a00b0f composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
package.json npm
  • @stdlib/array-base-to-accessor-array ^0.1.0 development
  • @stdlib/array-int32 ^0.1.1 development
  • @stdlib/array-uint8 ^0.1.1 development
  • @stdlib/assert-instance-of ^0.1.1 development
  • @stdlib/assert-is-string ^0.1.1 development
  • @stdlib/bench-harness ^0.1.2 development
  • istanbul ^0.4.1 development
  • tap-min git+https://github.com/Planeshifter/tap-min.git development
  • tape git+https://github.com/kgryte/tape.git#fix/globby development
  • @stdlib/array-base-assert-is-accessor-array ^0.1.0
  • @stdlib/array-base-copy ^0.1.0
  • @stdlib/array-base-resolve-getter ^0.1.0
  • @stdlib/array-dtype ^0.1.0
  • @stdlib/array-to-json ^0.1.0
  • @stdlib/assert-has-own-property ^0.1.1
  • @stdlib/assert-is-boolean ^0.1.1
  • @stdlib/assert-is-collection ^0.1.0
  • @stdlib/assert-is-integer ^0.1.0
  • @stdlib/assert-is-plain-object ^0.1.1
  • @stdlib/string-format ^0.1.1
  • @stdlib/types ^0.3.1
  • @stdlib/utils-define-nonenumerable-property ^0.1.1
  • @stdlib/utils-define-nonenumerable-read-only-accessor ^0.1.1
  • @stdlib/utils-define-nonenumerable-read-only-property ^0.1.1
  • @stdlib/utils-linked-list ^0.1.1