structurae
Advanced tools
Comparing version 0.0.2 to 0.0.3
/** | ||
* @typedef {Object} GridOptions | ||
* @property {number} rows the number of rows | ||
* @property {number} columns the number of columns | ||
* @property {*} pad the initial value of cells | ||
*/ | ||
/** | ||
* @typedef {ArrayConstructor| | ||
@@ -19,9 +12,23 @@ * Int8ArrayConstructor| | ||
* Float32ArrayConstructor| | ||
* Float64ArrayConstructor} IndexedCollection | ||
* Float64ArrayConstructor} IndexedCollectionConstructor | ||
*/ | ||
/** | ||
* @typedef {Array| | ||
* Int8Array| | ||
* Int8Array| | ||
* Uint8Array| | ||
* Uint8ClampedArray| | ||
* Int16Array| | ||
* Uint16Array| | ||
* Int32Array| | ||
* Uint32Array| | ||
* Float32Array| | ||
* Float64Array} IndexedCollection | ||
*/ | ||
/** | ||
* Creates a Grid class extending a given Array-like class. | ||
* | ||
* @param {IndexedCollection} Base | ||
* @param {IndexedCollectionConstructor} Base | ||
* @returns {Grid} | ||
@@ -34,3 +41,3 @@ * @example | ||
/** | ||
* @extends IndexedCollection | ||
* @extends IndexedCollectionConstructor | ||
*/ | ||
@@ -42,3 +49,7 @@ class Grid extends Base { | ||
* | ||
* @param {...(*|GridOptions)} args | ||
* @param {Object} [options] | ||
* @param {number} [options.rows=1] the number of rows | ||
* @param {number} [options.columns=2] the number of columns | ||
* @param {*} [options.pad=0] the initial value of cells | ||
* @param {IndexedCollection} [data] | ||
* @example | ||
@@ -58,15 +69,11 @@ * | ||
*/ | ||
constructor(...args) { | ||
let offset = 0; | ||
let pad = 0; | ||
if (args.length === 1 && args[0].columns) { | ||
const { columns, rows } = args[0]; | ||
offset = Grid.getOffset(columns); | ||
constructor(options = {}, data) { | ||
const { columns = 2, rows = 1, pad = 0 } = options; | ||
const offset = Grid.getOffset(columns); | ||
if (data && data.length) { | ||
super(data); | ||
} else { | ||
const length = rows << offset; | ||
super(length); | ||
if (Reflect.has(args[0], 'pad')) ([{ pad }] = args); | ||
this.fill(pad); | ||
} else { | ||
super(...args); | ||
} | ||
@@ -85,3 +92,3 @@ Object.defineProperties(this, { | ||
*/ | ||
setColumns(columns) { | ||
set columns(columns) { | ||
this.offset = Grid.getOffset(columns); | ||
@@ -91,2 +98,18 @@ } | ||
/** | ||
* Number of columns in the grid. | ||
* @type {number} | ||
*/ | ||
get columns() { | ||
return 1 << this.offset; | ||
} | ||
/** | ||
* Number of rows in the grid. | ||
* @type {number} | ||
*/ | ||
get rows() { | ||
return this.length >> this.offset; | ||
} | ||
/** | ||
* Returns an element from given coordinates. | ||
@@ -155,4 +178,3 @@ * | ||
toArrays(withPadding) { | ||
const rows = this.length >> this.offset; | ||
const columns = 1 << this.offset; | ||
const { rows, columns } = this; | ||
const begin = new Uint32Array(rows).map((b, i) => i << this.offset); | ||
@@ -164,3 +186,3 @@ const result = new Array(rows); | ||
if (withPadding) { | ||
result[i] = Base.from(this.slice(beginning, beginning + columns)); | ||
result[i] = this.slice(beginning, beginning + columns); | ||
continue; | ||
@@ -170,3 +192,3 @@ } | ||
if (this[j] !== this.pad) { | ||
result[i] = Base.from(this.slice(beginning, j + 1)); | ||
result[i] = this.slice(beginning, j + 1); | ||
break; | ||
@@ -181,2 +203,9 @@ } | ||
/** | ||
* @type {IndexedCollection} | ||
*/ | ||
static get [Symbol.species]() { | ||
return Base; | ||
} | ||
/** | ||
* @private | ||
@@ -214,9 +243,5 @@ * @param {number} columns | ||
columns = 1 << offset; | ||
const length = rows * columns; | ||
// create grid of the required length | ||
const grid = super.from({ length }); | ||
grid.fill(pad); | ||
grid.offset = offset; | ||
grid.pad = pad; | ||
const grid = new this({ rows, columns, pad }); | ||
@@ -223,0 +248,0 @@ // fill the grid with values from arrays |
{ | ||
"name": "structurae", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"coverage:report": "cat ./coverage/lcov.info | codecov", | ||
"doc:api": "jsdoc2md > doc/API.md" | ||
"doc:api": "jsdoc2md > doc/API.md", | ||
"benchmark": "node benchmark.js" | ||
}, | ||
@@ -39,2 +40,3 @@ "directories": { | ||
"@types/jest": "^23.3.12", | ||
"benchmark": "^2.1.4", | ||
"codecov": "^3.1.0", | ||
@@ -41,0 +43,0 @@ "eslint": "^5.11.1", |
@@ -9,5 +9,5 @@ # Structurae | ||
- Grid - extends built-in indexed collections to handle 2 dimensional data (e.g. nested arrays). | ||
- PackedInt - stores and operates on data in Numbers and BigInts treating them as bitfields. | ||
- SortedArray - extends built-in Array to efficiently handle sorted data. | ||
- [Grid](https://github.com/zandaqo/structurae#grid) - extends built-in indexed collections to handle 2 dimensional data (e.g. nested arrays). | ||
- [PackedInt](https://github.com/zandaqo/structurae#PackedInt) - stores and operates on data in Numbers and BigInts treating them as bitfields. | ||
- [SortedArray](https://github.com/zandaqo/structurae#SortedArray) - extends built-in Array to efficiently handle sorted data. | ||
@@ -43,11 +43,11 @@ ## Installation | ||
// only invoking the constructor with the options object will result in special behavior | ||
// otherwise all parameters are passed to the base class as they are: | ||
const arrayGrid = new ArrayGrid(1,2,3); | ||
arrayGrid.length | ||
//=> 3 | ||
// send data as the second parameter to instantiate a grid with data: | ||
const dataGrid = new ArrayGrid([{rows: 5, columns: 4 }, 1, 2, 3, 4, 5, 6, 7, 8]); | ||
grid.length | ||
//=> 20 | ||
grid[0] | ||
//=> 0 | ||
// to instantiate a grid with data simply invoke constructor with the data and set its columns size later: | ||
const dataGrid = new ArrayGrid(1, 2, 3, 4, 5, 6, 7, 8); | ||
dataGrid.setColumns(4); | ||
// you can change dimensions of the grid by setting columns number at any time: | ||
dataGrid.columns = 2; | ||
``` | ||
@@ -72,3 +72,3 @@ | ||
A grid be turned to and from an array of nested arrays using respectively `Grid#fromArrays` and `Grid#toArrays` methods: | ||
A grid can be turned to and from an array of nested arrays using respectively `Grid#fromArrays` and `Grid#toArrays` methods: | ||
```javascript | ||
@@ -75,0 +75,0 @@ const grid = ArrayGrid.fromArrays([[1,2], [3, 4]]); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48450
1211
8