@stdlib/array-bool

BooleanArray.

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

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.8%) to scientific vocabulary

Keywords

array binary bool boolean booleanarray data javascript mask node node-js nodejs stdlib structure typed typed-array types
Last synced: 4 months ago · JSON representation ·

Repository

BooleanArray.

Basic Info
Statistics
  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
array binary bool boolean booleanarray data javascript mask node node-js nodejs stdlib structure typed typed-array types
Created over 1 year ago · Last pushed 4 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security Notice

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!

BooleanArray

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

Boolean array.

## Installation ```bash npm install @stdlib/array-bool ``` 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 BooleanArray = require( '@stdlib/array-bool' ); ``` #### BooleanArray() Creates a boolean array. ```javascript var arr = new BooleanArray(); // returns ``` #### BooleanArray( length ) Creates a boolean array having a specified `length`. ```javascript var arr = new BooleanArray( 10 ); // returns var len = arr.length; // returns 10 ``` #### BooleanArray( booleanarray ) Creates a boolean array from another boolean array. ```javascript var arr1 = new BooleanArray( [ true, false, false, true ] ); // returns var arr2 = new BooleanArray( arr1 ); // returns var len = arr2.length; // returns 4 ``` #### BooleanArray( typedarray ) Creates a boolean array from a [typed array][@stdlib/array/typed]. ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var buf = new Uint8Array( [ 1, 0, 0, 1 ] ); // returns [ 1, 0, 0, 1 ] var arr = new BooleanArray( buf ); // returns var len = arr.length; // returns 4 ``` #### BooleanArray( obj ) Creates a boolean array from an array-like object or iterable. ```javascript // From an array of booleans: var arr1 = new BooleanArray( [ true, false, false, true ] ); // returns var len = arr1.length; // returns 4 // From an array containing non-booleans: var arr2 = new BooleanArray( [ {}, null, '', 4 ] ); len = arr2.length; // returns 4 ``` #### BooleanArray( buffer\[, byteOffset\[, length]] ) Returns a boolean array view of an [`ArrayBuffer`][@stdlib/array/buffer]. ```javascript var ArrayBuffer = require( '@stdlib/array-buffer' ); var buf = new ArrayBuffer( 240 ); var arr1 = new BooleanArray( buf ); // returns var len = arr1.length; // returns 240 var arr2 = new BooleanArray( buf, 8 ); // returns len = arr2.length; // returns 232 var arr3 = new BooleanArray( buf, 8, 20 ); // returns len = arr3.length; // returns 20 ``` * * * ### Properties #### BooleanArray.BYTES_PER_ELEMENT Static property returning the size (in bytes) of each array element. ```javascript var nbytes = BooleanArray.BYTES_PER_ELEMENT; // returns 1 ``` #### BooleanArray.name Static property returning the constructor name. ```javascript var str = BooleanArray.name; // returns 'BooleanArray' ``` #### BooleanArray.prototype.buffer Pointer to the underlying data buffer. ```javascript var arr = new BooleanArray( 2 ); // returns var buf = arr.buffer; // returns ``` #### BooleanArray.prototype.byteLength Size (in bytes) of the array. ```javascript var arr = new BooleanArray( 10 ); // returns var nbytes = arr.byteLength; // returns 10 ``` #### BooleanArray.prototype.byteOffset Offset (in bytes) of the array from the start of its underlying `ArrayBuffer`. ```javascript var ArrayBuffer = require( '@stdlib/array-buffer' ); var arr = new BooleanArray( 10 ); // returns var offset = arr.byteOffset; // returns 0 var buf = new ArrayBuffer( 240 ); arr = new BooleanArray( buf, 64 ); // returns offset = arr.byteOffset; // returns 64 ``` #### BooleanArray.prototype.BYTES_PER_ELEMENT Size (in bytes) of each array element. ```javascript var arr = new BooleanArray( 10 ); // returns var nbytes = arr.BYTES_PER_ELEMENT; // returns 1 ``` #### BooleanArray.prototype.length Number of array elements. ```javascript var arr = new BooleanArray( 10 ); // returns var len = arr.length; // returns 10 ``` * * * ### Methods #### BooleanArray.from( src\[, clbk\[, thisArg]] ) Creates a new boolean array from an array-like object or an iterable. ```javascript var arr = BooleanArray.from( [ true, false ] ); // returns var len = arr.length; // returns 2 ``` To invoke a function for each `src` value, provide a callback function. ```javascript function map( v ) { return !v; } // Create a source array: var src = [ true, false ]; // Create a new boolean array by inverting the source array: var arr = BooleanArray.from( src, map ); // returns var len = arr.length; // returns 2 var v = arr.get( 0 ); // returns false v = arr.get( 1 ); // returns true ``` A callback function is provided two arguments: - **value**: source value. - **index**: source index. To set the callback execution context, provide a `thisArg`. ```javascript function map( v ) { this.count += 1; return !v; } // Create a source array: var src = [ true, false ]; // Define an execution context: var ctx = { 'count': 0 }; // Create a new boolean array by inverting the source array: var arr = BooleanArray.from( src, map, ctx ); // returns var len = arr.length; // returns 2 var n = ctx.count; // returns 2 ``` #### BooleanArray.of( element0\[, element1\[, ...elementN]] ) Creates a new boolean array from a variable number of arguments. ```javascript var arr = BooleanArray.of( true, false, false, true ); // returns var len = arr.length; // returns 4 ``` #### BooleanArray.prototype.at( i ) Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var v = arr.at( 0 ); // returns true v = arr.at( -1 ); // returns true ``` If provided an out-of-bounds index, the method returns `undefined`. ```javascript var arr = new BooleanArray( 10 ); var v = arr.at( 100 ); // returns undefined v = arr.at( -100 ); // returns undefined ``` #### BooleanArray.prototype.copyWithin( target, start\[, end] ) Copies a sequence of elements within the array starting at `start` and ending at `end` (non-inclusive) to the position starting at `target`. ```javascript var arr = new BooleanArray( 4 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( false, 2 ); arr.set( true, 3 ); var v = arr.get( 0 ); // returns true v = arr.get( 1 ); // returns false // Copy the last two elements to the first two elements: arr.copyWithin( 0, 2 ); v = arr.get( 0 ); // returns false v = arr.get( 1 ); // returns true ``` By default, `end` equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an `end` argument. ```javascript var arr = new BooleanArray( 4 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( false, 2 ); arr.set( true, 3 ); var v = arr.get( 2 ); // returns false v = arr.get( 3 ); // returns true // Copy the first two elements to the last two elements: arr.copyWithin( 2, 0, 2 ); v = arr.get( 2 ); // returns true v = arr.get( 3 ); // returns false ``` When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example: ```javascript var arr = new BooleanArray( 4 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( false, 2 ); arr.set( true, 3 ); var v = arr.get( 2 ); // returns false v = arr.get( 3 ); // returns true // Copy the first two elements to the last two elements using negative indices: arr.copyWithin( -2, -4, -2 ); v = arr.get( 2 ); // returns true v = arr.get( 3 ); // returns false ``` #### BooleanArray.prototype.entries() Returns an iterator for iterating over array key-value pairs. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var it = arr.entries(); var v = it.next().value; // returns [ 0, true ] v = it.next().value; // returns [ 1, false ] v = it.next().value; // returns [ 2, true ] var bool = it.next().done; // returns true ``` The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: - **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. - **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. #### BooleanArray.prototype.every( predicate\[, thisArg] ) Returns a boolean indicating whether all elements pass a test. ```javascript function predicate( v ) { return v === true; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( true, 1 ); arr.set( true, 2 ); var bool = arr.every( predicate ); // returns true ``` The `predicate` function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; return v === true; } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( true, 0 ); arr.set( true, 1 ); arr.set( true, 2 ); var bool = arr.every( predicate, context ); // returns true var count = context.count; // returns 3 ``` #### BooleanArray.prototype.fill( value\[, start\[, end]] ) Returns a modified typed array filled with a fill value. ```javascript var arr = new BooleanArray( 3 ); // Set all elements to the same value: arr.fill( true ); var v = arr.get( 0 ); // returns true v = arr.get( 1 ); // returns true v = arr.get( 2 ); // returns true // Fill all elements starting from the second element: arr.fill( false, 1 ); v = arr.get( 1 ); // returns false v = arr.get( 2 ); // returns false // Fill all elements from first element until the second-to-last element: arr.fill( false, 0, 2 ); v = arr.get( 0 ); // returns false v = arr.get( 1 ); // returns false ``` When a `start` and/or `end` index is negative, the respective index is determined relative to the last array element. ```javascript var arr = new BooleanArray( 3 ); // Set all array elements, except the last element, to the same value: arr.fill( true, 0, -1 ); var v = arr.get( 0 ); // returns true v = arr.get( 2 ); // returns false ``` #### BooleanArray.prototype.filter( predicate\[, thisArg] ) Returns a new array containing the elements of an array which pass a test implemented by a predicate function. ```javascript function predicate( v ) { return ( v === true ); } var arr = new BooleanArray( 3 ); // Set the first three elements: arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.filter( predicate ); // returns var len = out.length; // returns 2 var v = out.get( 0 ); // returns true v = out.get( 1 ); // return true ``` The `predicate` function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function predicate( v, i ) { this.count += 1; return ( v === true ); } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.filter( predicate, context ); // returns var len = out.length; // returns 2 var count = context.count; // returns 3 ``` #### BooleanArray.prototype.find( predicate\[, thisArg] ) Returns the first element in an array for which a predicate function returns a truthy value. ```javascript function predicate( v ) { return v === true; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var v = arr.find( predicate ); // returns true ``` The `predicate` function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; return ( v === true ); } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( false, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var z = arr.find( predicate, context ); // returns true var count = context.count; // returns 3 ``` #### BooleanArray.prototype.findIndex( predicate\[, thisArg] ) Returns the index of the first element in an array for which a predicate function returns a truthy value. ```javascript function predicate( v ) { return v === true; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var v = arr.findIndex( predicate ); // returns 0 ``` The `predicate` function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; return ( v === true ); } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( false, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var z = arr.findIndex( predicate, context ); // returns 2 var count = context.count; // returns 3 ``` #### BooleanArray.prototype.findLast( predicate\[, thisArg] ) Returns the last element in an array for which a predicate function returns a truthy value. ```javascript function predicate( v ) { return v === true; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var v = arr.findLast( predicate ); // returns true ``` The `predicate` function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; return ( v === true ); } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( true, 0 ); arr.set( false, 1 ); arr.set( false, 2 ); var z = arr.findLast( predicate, context ); // returns true var count = context.count; // returns 3 ``` #### BooleanArray.prototype.findLastIndex( predicate\[, thisArg] ) Returns the index of the last element in an array for which a predicate function returns a truthy value. ```javascript function predicate( v ) { return v === true; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var v = arr.findLastIndex( predicate ); // returns 2 ``` The `predicate` function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; return ( v === true ); } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( true, 0 ); arr.set( false, 1 ); arr.set( false, 2 ); var z = arr.findLastIndex( predicate, context ); // returns 0 var count = context.count; // returns 3 ``` #### BooleanArray.prototype.forEach( callbackFn\[, thisArg] ) Invokes a function once for each array element. ```javascript function log( v, i ) { console.log( '%s: %s', i.toString(), v.toString() ); } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.forEach( log ); /* => 0: true 1: false 2: true */ ``` The invoked function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function fcn( v, i ) { this.count += 1; console.log( '%s: %s', i.toString(), v.toString() ); } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.forEach( fcn, context ); var count = context.count; // returns 3 ``` #### BooleanArray.prototype.get( i ) Returns an array element located at a nonnegative integer position (index) `i`. ```javascript var arr = new BooleanArray( 10 ); // Set the first element: arr.set( true, 0 ); // Get the first element: var v = arr.get( 0 ); // returns true ``` If provided an out-of-bounds index, the method returns `undefined`. ```javascript var arr = new BooleanArray( 10 ); var v = arr.get( 100 ); // returns undefined ``` #### BooleanArray.prototype.includes( searchElement\[, fromIndex] ) Returns a boolean indicating whether an array includes a provided value. ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( true, 3 ); arr.set( true, 4 ); var bool = arr.includes( true ); // returns true bool = arr.includes( false, 2 ); // returns false ``` #### BooleanArray.prototype.indexOf( searchElement\[, fromIndex] ) Returns the first index at which a given element can be found. ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( true, 3 ); arr.set( true, 4 ); var idx = arr.indexOf( true ); // returns 0 idx = arr.indexOf( false, 1 ); // returns 1 idx = arr.indexOf( true, -3 ); // returns 2 ``` If `searchElement` is not present in the array, the method returns `-1`. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( true, 1 ); arr.set( true, 2 ); var idx = arr.indexOf( false ); // returns -1 ``` #### BooleanArray.prototype.join( \[separator] ) Returns a new string by concatenating all array elements. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var str = arr.join(); // returns 'true,false,true' ``` By default, the method separates serialized array elements with a comma. To use an alternative separator, provide a `separator` string. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var str = arr.join( '|' ); // returns 'true|false|true' ``` #### BooleanArray.prototype.keys() Returns an iterator for iterating over each index key in a typed array. ```javascript var arr = new BooleanArray( 2 ); arr.set( true, 0 ); arr.set( false, 1 ); var iter = arr.keys(); var v = iter.next().value; // returns 0 v = iter.next().value; // returns 1 var bool = iter.next().done; // returns true ``` The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: - **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. - **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. #### BooleanArray.prototype.lastIndexOf( searchElement\[, fromIndex] ) Returns the last index at which a given element can be found. ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( true, 1 ); arr.set( true, 2 ); arr.set( false, 3 ); arr.set( true, 4 ); var idx = arr.lastIndexOf( true ); // returns 4 idx = arr.lastIndexOf( false, 3 ); // returns 3 idx = arr.lastIndexOf( true, -3 ); // returns 2 ``` If `searchElement` is not present in the array, the method returns `-1`. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( true, 1 ); arr.set( true, 2 ); var idx = arr.lastIndexOf( false ); // returns -1 ``` #### BooleanArray.prototype.map( callbackFn\[, thisArg] ) Returns a new array with each element being the result of a provided callback function. ```javascript function invert( v ) { return !v; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.map( invert ); // returns var z = out.get( 0 ); // returns false z = out.get( 1 ); // returns true z = out.get( 2 ); // returns false ``` The callback function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function invert( v, i ) { this.count += i; return !v; } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.map( invert, context ); // returns var count = context.count; // returns 3; ``` #### BooleanArray.prototype.reduce( reducerFn\[, initialValue] ) Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion. ```javascript function reducer( acc, v ) { return ( acc && v ); } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.reduce( reducer ); // returns false ``` The reducer function is provided four arguments: - **acc**: accumulated result. - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument. ```javascript function reducer( acc, v ) { if ( v ) { return acc + 1; } return acc; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.reduce( reducer, 0 ); // returns 2 ``` #### BooleanArray.prototype.reduceRight( reducerFn\[, initialValue] ) Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion. ```javascript function reducer( acc, v ) { return ( acc && v ); } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.reduceRight( reducer ); // returns false ``` The reducer function is provided four arguments: - **acc**: accumulated result. - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. By default, the function initializes the accumulated result to the last element in the array and passes the second-last array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument. ```javascript function reducer( acc, v ) { if ( v ) { return acc + 1; } return acc; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.reduceRight( reducer, 0 ); // returns 2 ``` #### BooleanArray.prototype.reverse() Reverses an array in-place. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( false, 2 ); var out = arr.reverse(); // returns var v = out.get( 0 ); // returns false v = out.get( 1 ); // returns false v = out.get( 2 ); // returns true ``` #### BooleanArray.prototype.set( v\[, i] ) Sets one or more array elements. ```javascript var arr = new BooleanArray( 10 ); // Get the first element: var v = arr.get( 0 ); // returns false // Set the first element: arr.set( true ); // Get the first element: v = arr.get( 0 ); // returns true ``` By default, the method sets array elements starting at position (index) `i = 0`. To set elements starting elsewhere in the array, provide an index argument `i`. ```javascript var arr = new BooleanArray( 10 ); // Get the fifth element: var v = arr.get( 4 ); // returns false // Set the fifth element: arr.set( true, 4 ); // Get the fifth element: v = arr.get( 4 ); // returns true ``` In addition to providing a single value, to set one or more array elements, provide an array-like object containing truthy and falsy values ```javascript var arr = new BooleanArray( 10 ); // Define an array of values: var buf = [ '', 1, null ]; // Set the fifth, sixth, and seventh elements: arr.set( buf, 4 ); // Get the sixth element: var v = arr.get( 5 ); // returns true ``` A few notes: - If `i` is out-of-bounds, the method throws an error. - If a target array cannot accommodate all values (i.e., the length of source array plus `i` exceeds the target array length), the method throws an error. - If provided a [typed array][@stdlib/array/typed] which shares an [`ArrayBuffer`][@stdlib/array/buffer] with the target array, the method will intelligently copy the source range to the destination range. #### BooleanArray.prototype.slice( \[start\[, end]] ) Copies a portion of a typed array to a new typed array. ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( false, 3 ); arr.set( true, 4 ); var out = arr.slice(); // returns var len = out.length; // returns 5 var bool = out.get( 0 ); // returns true bool = out.get( len-1 ); // returns true ``` By default, the method returns a typed array beginning with the first array element. To specify an alternative array index at which to begin, provide a `start` index (inclusive). ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( false, 3 ); arr.set( true, 4 ); var out = arr.slice( 1 ); // returns var len = out.length; // returns 4 var bool = out.get( 0 ); // returns false bool = out.get( len-1 ); // returns true ``` By default, the method returns a typed array which includes all array elements after `start`. To limit the number of array elements after `start`, provide an `end` index (exclusive). ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( false, 3 ); arr.set( true, 4 ); var out = arr.slice( 1, -2 ); // returns var len = out.length; // returns 2 var bool = out.get( 0 ); // returns false bool = out.get( len-1 ); // returns true ``` #### BooleanArray.prototype.some( predicate\[, thisArg] ) Returns a boolean indicating whether at least one element passes a test. ```javascript function predicate( v ) { return v === true; } var arr = new BooleanArray( 3 ); arr.set( false, 0 ); arr.set( true, 1 ); arr.set( false, 2 ); var bool = arr.some( predicate ); // returns true ``` The `predicate` function is provided three arguments: - **value**: current array element. - **index**: current array element index. - **arr**: the array on which this method was called. To set the function execution context, provide a `thisArg`. ```javascript function predicate( v ) { this.count += 1; return v === true; } var arr = new BooleanArray( 3 ); var context = { 'count': 0 }; arr.set( false, 0 ); arr.set( true, 1 ); arr.set( false, 2 ); var bool = arr.some( predicate, context ); // returns true var count = context.count; // returns 2 ``` #### BooleanArray.prototype.sort( \[compareFcn] ) Sorts an array in-place. ```javascript function compare( a, b ) { if ( a === false ) { if ( b === false ) { return 0; } return 1; } if ( b === true ) { return 0; } return -1; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.sort( compare ); var v = arr.get( 0 ); // returns true v = arr.get( 1 ); // returns true v = arr.get( 2 ); // returns false ``` The `compareFcn` determines the order of the elements. The function is called with the following arguments: - **a**: the first boolean value for comparison. - **b**: the second boolean value for comparison. The function should return a number where: - a negative value indicates that `a` should come before `b`. - a positive value indicates that `a` should come after `b`. - zero or `NaN` indicates that `a` and `b` are considered equal. #### BooleanArray.prototype.subarray( \[begin\[, end]] ) Creates a new typed array view over the same underlying [`ArrayBuffer`][@stdlib/array/buffer] and with the same underlying data type as the host array. ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( false, 3 ); arr.set( true, 4 ); var subarr = arr.subarray(); // returns var len = subarr.length; // returns 5 var bool = subarr.get( 0 ); // returns true bool = subarr.get( len-1 ); // returns true ``` By default, the method creates a typed array view beginning with the first array element. To specify an alternative array index at which to begin, provide a `begin` index (inclusive). ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( false, 3 ); arr.set( true, 4 ); var subarr = arr.subarray( 1 ); // returns var len = subarr.length; // returns 4 var bool = subarr.get( 0 ); // returns false bool = subarr.get( len-1 ); // returns true ``` By default, the method creates a typed array view which includes all array elements after `begin`. To limit the number of array elements after `begin`, provide an `end` index (exclusive). ```javascript var arr = new BooleanArray( 5 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); arr.set( false, 3 ); arr.set( true, 4 ); var subarr = arr.subarray( 1, -2 ); // returns var len = subarr.length; // returns 2 var bool = subarr.get( 0 ); // returns false bool = subarr.get( len-1 ); // returns true ``` #### BooleanArray.prototype.toLocaleString( \[locales\[, options]] ) Serializes an array as a locale-specific string. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var str = arr.toLocaleString(); // returns 'true,false,true' ``` The method supports the following arguments: - **locales**: a string with a BCP 47 language tag or an array of such strings. - **options**: configuration properties. #### BooleanArray.prototype.toReversed() Returns a new typed array containing the elements in reversed order. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( false, 2 ); var out = arr.toReversed(); // returns var v = out.get( 0 ); // returns false v = out.get( 1 ); // returns false v = out.get( 2 ); // returns true ``` #### BooleanArray.prototype.toSorted( \[compareFcn] ) Returns a new typed array containing the elements in sorted order. ```javascript function compare( a, b ) { if ( a === false ) { if ( b === false ) { return 0; } return 1; } if ( b === true ) { return 0; } return -1; } var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var out = arr.sort( compare ); // returns var v = out.get( 0 ); // returns true v = out.get( 1 ); // returns true v = out.get( 2 ); // returns false ``` The `compareFcn` determines the order of the elements. The function is called with the following arguments: - **a**: the first boolean value for comparison. - **b**: the second boolean value for comparison. The function should return a number where: - a negative value indicates that `a` should come before `b`. - a positive value indicates that `a` should come after `b`. - zero or `NaN` indicates that `a` and `b` are considered equal. #### BooleanArray.prototype.toString() Serializes an array as a string. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 2 ); var str = arr.toString(); // returns 'true,false,true' ``` #### BooleanArray.prototype.values() Returns an iterator for iterating over each value in a typed array. ```javascript var arr = new BooleanArray( 2 ); arr.set( true, 0 ); arr.set( false, 1 ); var iter = arr.values(); var v = iter.next().value; // returns true v = iter.next().value; // returns false var bool = iter.next().done; // returns true ``` The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: - **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. - **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. #### BooleanArray.prototype.with( index, value ) Returns a new typed array with the element at a provided index replaced with a provided value. ```javascript var arr = new BooleanArray( 3 ); arr.set( true, 0 ); arr.set( false, 1 ); arr.set( true, 1 ); var out = arr.with( 0, false ); // returns var v = out.get( 0 ); // returns false ```
* * * ## Notes - While a `BooleanArray` _strives_ to maintain (but does not **guarantee**) consistency with [typed arrays][@stdlib/array/typed], significant deviations from ECMAScript-defined [typed array][@stdlib/array/typed] behavior are as follows: - The constructor does **not** require the `new` operator. - Accessing array elements using bracket syntax (e.g., `X[i]`) is **not** supported. Instead, one **must** use the `.get()` method.
* * * ## Examples ```javascript var Uint8Array = require( '@stdlib/array-uint8' ); var logEach = require( '@stdlib/console-log-each' ); var BooleanArray = require( '@stdlib/array-bool' ); // Create a boolean array by specifying a length: var out = new BooleanArray( 3 ); logEach( '%s', out ); // Create a boolean array from an array of booleans: var arr = [ true, false, true ]; out = new BooleanArray( arr ); logEach( '%s', out ); // Create a boolean array from an array buffer: arr = new Uint8Array( [ 1, 0, 1, 1, 0, 1 ] ); out = new BooleanArray( arr.buffer ); logEach( '%s', out ); // Create a boolean array from an array buffer view: arr = new Uint8Array( [ 1, 0, 1, 1, 0, 1 ] ); out = new BooleanArray( arr.buffer, 1, 2 ); logEach( '%s', out ); console.log( '%s', false ); ```
* * * ## 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: 48
Last Year
  • Push event: 48

Committers

Last synced: 8 months ago

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

Issues and Pull Requests

Last synced: 4 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 3,847 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 2
  • Total maintainers: 4
npmjs.org: @stdlib/array-bool

BooleanArray.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.1.0
    published over 1 year ago
  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 3,847 Last month
Rankings
Downloads: 13.6%
Average: 27.4%
Dependent repos count: 27.9%
Dependent packages count: 40.5%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 4 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 5d5d22a31266ced268874388b861e4b58bb5c2f3 composite
  • distributhor/workflow-webhook 48a40b380ce4593b6a6676528cd005986ae56629 composite
.github/workflows/productionize.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 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
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • JS-DevTools/npm-publish 4b07b26a2f6e0a51846e1870223e545bae91c552 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • styfle/cancel-workflow-action 85880fa0301c86cca9da44039ee3bb12d3bedbfa composite
.github/workflows/test.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/test_bundles.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • denoland/setup-deno 041b854f97b325bd60e53e9dc2de9cb9f9ac0cba composite
.github/workflows/test_coverage.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • codecov/codecov-action 84508663e988701840491b86de86b666e8a86bed composite
  • distributhor/workflow-webhook 48a40b380ce4593b6a6676528cd005986ae56629 composite
.github/workflows/test_install.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
package.json npm
  • @stdlib/array-buffer ^0.2.1 development
  • @stdlib/array-float32 ^0.2.1 development
  • @stdlib/assert-has-own-property ^0.2.1 development
  • @stdlib/assert-is-boolean ^0.2.1 development
  • @stdlib/bench-harness ^0.2.1 development
  • @stdlib/console-log-each ^0.2.1 development
  • @stdlib/math-base-special-pow ^0.2.1 development
  • @stdlib/random-base-randu ^0.2.1 development
  • istanbul ^0.4.1 development
  • proxyquire ^2.0.0 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-accessor-getter ^0.2.1
  • @stdlib/array-base-getter ^0.2.1
  • @stdlib/array-uint8 ^0.2.1
  • @stdlib/assert-has-iterator-symbol-support ^0.2.1
  • @stdlib/assert-is-arraybuffer ^0.2.1
  • @stdlib/assert-is-collection ^0.2.1
  • @stdlib/assert-is-function ^0.2.1
  • @stdlib/assert-is-nonnegative-integer ^0.2.1
  • @stdlib/assert-is-object ^0.2.1
  • @stdlib/boolean-ctor ^0.2.1
  • @stdlib/error-tools-fmtprodmsg ^0.2.1
  • @stdlib/string-format ^0.2.1
  • @stdlib/symbol-iterator ^0.2.1
  • @stdlib/types ^0.3.2
  • @stdlib/utils-define-nonenumerable-read-only-accessor ^0.2.2
  • @stdlib/utils-define-nonenumerable-read-only-property ^0.2.1