structurae
Advanced tools
Comparing version
@@ -140,3 +140,3 @@ // Type definitions for structurae | ||
private offsets: object; | ||
private stringView: Uint8Array; | ||
private stringView: StringView; | ||
@@ -146,3 +146,3 @@ constructor(fields: RecordField[], size: number, buffer?: ArrayBuffer, byteOffset?: number, byteLength?: number); | ||
set(index: number, field: string, value: any, littleEndian?: boolean): this; | ||
getString(offset: number, littleEndian: boolean, size: number): Uint8Array; | ||
getString(offset: number, littleEndian: boolean, size: number): StringView; | ||
setString(offset: number, value: Collection): void; | ||
@@ -155,4 +155,4 @@ toObject(index: number): object; | ||
constructor(size: number); | ||
acquire(): number; | ||
release(index: number): void; | ||
get(): number; | ||
free(index: number): void; | ||
} | ||
@@ -177,2 +177,18 @@ | ||
static getByteSize(string: string): number; | ||
} | ||
export declare class BinaryHeap extends Array { | ||
heapify(): this; | ||
left(index: number): any; | ||
parent(index: number): any; | ||
replace(item: any): any; | ||
right(index: number): any; | ||
update(index: number): void; | ||
private has(index: number): boolean; | ||
private siftDown(start: number): void; | ||
private siftUp(start: number): void; | ||
static compare(a: any, b: any): boolean; | ||
static getLeftIndex(index: number): number; | ||
static getParentIndex(index: number): number; | ||
static getRightIndex(index: number): number; | ||
} |
const BitField = require('./lib/bit-field'); | ||
const GridMixin = require('./lib/grid'); | ||
const BinaryHeap = require('./lib/binary-heap.js'); | ||
const Pool = require('./lib/pool'); | ||
@@ -27,2 +28,3 @@ const RecordArray = require('./lib/record-array'); | ||
GridMixin, | ||
BinaryHeap, | ||
Pool, | ||
@@ -29,0 +31,0 @@ RecordArray, |
@@ -69,3 +69,3 @@ /** | ||
const stringView = hasString ? new Uint8Array(this.buffer) : undefined; | ||
const stringView = hasString ? new StringView(this.buffer) : undefined; | ||
Object.defineProperties(this, { | ||
@@ -188,4 +188,3 @@ fields: { value: fields }, | ||
getString(offset, size) { | ||
const subarray = this.stringView.subarray(offset, offset + size); | ||
return new StringView(subarray.buffer, subarray.byteOffset, subarray.byteLength); | ||
return this.stringView.subarray(offset, offset + size); | ||
} | ||
@@ -192,0 +191,0 @@ |
@@ -71,2 +71,13 @@ const SortedMixin = require('./sorted-collection'); | ||
/** | ||
* Sorts the array with a provided compare function. | ||
* | ||
* @private | ||
* @param {Comparator} compareFunction the function to use for comparison | ||
* @returns {this} | ||
*/ | ||
sort(compareFunction = this.compare) { | ||
return super.sort(compareFunction); | ||
} | ||
/** | ||
* Changes the array by removing existing elements and adding new ones. | ||
@@ -73,0 +84,0 @@ * |
@@ -136,13 +136,2 @@ /** | ||
/** | ||
* Sorts the array with a provided compare function. | ||
* | ||
* @private | ||
* @param {Comparator} compareFunction the function to use for comparison | ||
* @returns {this} | ||
*/ | ||
sort(compareFunction = this.compare) { | ||
return super.sort(compareFunction); | ||
} | ||
/** | ||
* Creates a new SortedCollection from a given array-like object. | ||
@@ -149,0 +138,0 @@ * |
@@ -7,3 +7,3 @@ const { searchNaive, searchShiftOr } = require('./algorithms'); | ||
/** | ||
* Extends Uint8Array to handle C-like representation of UTF-8 strings. | ||
* Extends Uint8Array to handle C-like representation of UTF-8 encoded strings. | ||
* | ||
@@ -17,2 +17,6 @@ * @extends Uint8Array | ||
* @yields {string} | ||
* @example | ||
* const stringView = StringView.fromString('abc😀'); | ||
* [...stringView.characters()] | ||
* //=> ['a', 'b', 'c', '😀'] | ||
*/ | ||
@@ -33,2 +37,8 @@ * characters() { | ||
* @returns {string} a string representing the character | ||
* @example | ||
* const stringView = StringView.fromString('abc😀'); | ||
* stringView.charAt(0); | ||
* //=> 'a' | ||
* stringView.charAt(3); | ||
* //=> '😀' | ||
*/ | ||
@@ -79,2 +89,8 @@ charAt(index = 0) { | ||
* @returns {StringView} | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a'); | ||
* const pattern = StringView.fromString('a'); | ||
* const replacement = StringView.fromString('d'); | ||
* stringView.replace(pattern, replacement).toString(); | ||
* //=> 'dbc😀d' | ||
*/ | ||
@@ -96,2 +112,6 @@ replace(pattern, replacement) { | ||
* @returns {StringView} | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a'); | ||
* stringView.reverse().toString(); | ||
* //=> 'a😀cba' | ||
*/ | ||
@@ -133,2 +153,7 @@ reverse() { | ||
* @returns {number} the index of the first occurrence of the specified value | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a'); | ||
* const searchValue = StringView.fromString('😀'); | ||
* stringView.search(searchValue); | ||
* //=> 3 | ||
*/ | ||
@@ -146,2 +171,8 @@ search(searchValue, fromIndex = 0) { | ||
* @type {number} | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a'); | ||
* stringView.size | ||
* //=> 5 | ||
* stringView.length | ||
* //=> 8 | ||
*/ | ||
@@ -163,2 +194,8 @@ get size() { | ||
* @returns {string} a new string containing the specified part of the given string | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a'); | ||
* stringView.substring(0, 4); | ||
* //=> 'abc😀' | ||
* stringView.substring(2); | ||
* //=> 'c😀a' | ||
*/ | ||
@@ -199,2 +236,8 @@ substring(indexStart, indexEnd = this.size) { | ||
* @returns {string} | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a'); | ||
* stringView.toString(); | ||
* //=> 'abc😀a' | ||
* stringView == 'abc😀a' | ||
* //=> true | ||
*/ | ||
@@ -209,2 +252,8 @@ toString() { | ||
* @returns {StringView} | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a', 10); | ||
* stringView | ||
* //=> StringView [ 97, 98, 99, 240, 159, 152, 128, 97, 0, 0 ] | ||
* stringView.trim(); | ||
* //=> StringView [ 97, 98, 99, 240, 159, 152, 128, 97 ] | ||
*/ | ||
@@ -222,2 +271,10 @@ trim() { | ||
* @returns {StringView} a new StringView | ||
* @example | ||
* const stringView = StringView.fromString('abc😀a'); | ||
* stringView | ||
* //=> StringView [ 97, 98, 99, 240, 159, 152, 128, 97 ] | ||
* | ||
* const stringView = StringView.fromString('abc😀a', 10); | ||
* stringView | ||
* //=> StringView [ 97, 98, 99, 240, 159, 152, 128, 97, 0, 0 ] | ||
*/ | ||
@@ -231,10 +288,13 @@ static fromString(string, size) { | ||
} | ||
return new this(encoded); | ||
return new this(encoded.buffer); | ||
} | ||
/** | ||
* Returns the size in bytes of a given string. | ||
* Returns the size in bytes of a given string without encoding it. | ||
* | ||
* @param {string} string the string to check | ||
* @returns {number} the size in bytes | ||
* @example | ||
* const stringView = StringView.getByteSize('abc😀a'); | ||
* //=> 8 | ||
*/ | ||
@@ -241,0 +301,0 @@ static getByteSize(string) { |
{ | ||
"name": "structurae", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -38,8 +38,8 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.12", | ||
"@types/jest": "^23.3.13", | ||
"benchmark": "^2.1.4", | ||
"codecov": "^3.1.0", | ||
"eslint": "^5.11.1", | ||
"eslint": "^5.13.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"jest": "^24.0.0", | ||
@@ -60,3 +60,5 @@ "jsdoc-to-markdown": "^4.0.1" | ||
], | ||
"setupFilesAfterEnv": ["<rootDir>/test/setup.js"] | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/test/setup.js" | ||
] | ||
}, | ||
@@ -63,0 +65,0 @@ "engines": { |
@@ -9,7 +9,8 @@ # Structurae | ||
- [BitField](https://github.com/zandaqo/structurae#BitField) - stores and operates on data in Numbers and BigInts treating them as bitfields. | ||
- [Grid](https://github.com/zandaqo/structurae#grid) - extends built-in indexed collections to handle 2 dimensional data (e.g. nested arrays). | ||
- [BitField](https://github.com/zandaqo/structurae#BitField) - stores and operates on data in Numbers and BigInts treating them as bitfields. | ||
- [Pool](https://github.com/zandaqo/structurae#Pool) - manages availability of objects in object pools. | ||
- [RecordArray](https://github.com/zandaqo/structurae#RecordArray) - extends DataView to use ArrayBuffer as an array of records or C-like structs. | ||
- [SortedCollection](https://github.com/zandaqo/structurae#SortedCollection) & [SortedArray](https://github.com/zandaqo/structurae#SortedArray) - extends built-in Array or TypedArrays to efficiently handle sorted data. | ||
- [StringView](https://github.com/zandaqo/structurae#StringView) - extends Uint8Array to handle C-like representation of UTF-8 encoded strings. | ||
@@ -299,13 +300,14 @@ ## Installation | ||
people.toObject(0); | ||
//=> { age: 10, score: 5.0 } | ||
//=> { age: 10, score: 5.0, name: '' } | ||
``` | ||
String type is represented with Uint8Arrays. You can use TextEncoder/TextDecoder API to convert them to and from strings. | ||
The String type is handled with [StringView](https://github.com/zandaqo/structurae#StringView). | ||
You can use its methods to convert them to and from strings. | ||
```javascript | ||
people.get(0, 'name'); | ||
//=> Uint8Array(10) [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | ||
const name = new TextEncoder().encode('Smith'); | ||
//=> StringView(10) [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | ||
const name = StringView.fromString('Smith'); | ||
people.set(0, name).get(0, 'name'); | ||
//=> Uint8Array(10) [83, 109, 105, 116, 104, 0, 0, 0, 0, 0] | ||
new TextDecoder().decode(people.get(0, 'name')); | ||
//=> StringView(10) [83, 109, 105, 116, 104, 0, 0, 0, 0, 0] | ||
people.get(0, 'name').toString(); | ||
//=> Smith | ||
@@ -411,2 +413,48 @@ ``` | ||
### StringView | ||
Encoding API (available both in modern browsers and Node.js) allows us to convert JavaScript strings to | ||
(and from) UTF-8 encoded stream of bytes represented by a Uint8Array. StringView extends Uint8Array with string related methods | ||
and relies on Encoding API internally for conversions. | ||
You can use `StringView.fromString` to create an encoded string, and `StringView#toString` to convert it back to a string: | ||
```javascript | ||
const stringView = StringView.fromString('abc😀a'); | ||
//=> StringView [ 97, 98, 99, 240, 159, 152, 128, 97 ] | ||
stringView.toString(); | ||
//=> 'abc😀a' | ||
stringView == 'abc😀a'; | ||
//=> true | ||
``` | ||
While the array itself holds code points, StringView provides methods to operate on characters of the underlying string: | ||
```javascript | ||
const stringView = StringView.fromString('abc😀'); | ||
stringView.length; // length of the view in bytes | ||
//=> 8 | ||
stringView.size; // the amount of characters in the string | ||
//=> 4 | ||
stringView.charAt(0); // get the first character in the string | ||
//=> 'a' | ||
stringView.charAt(3); // get the fourth character in the string | ||
//=> '😀' | ||
[...stringView.characters()] // iterate over characters | ||
//=> ['a', 'b', 'c', '😀'] | ||
stringView.substring(0, 4); | ||
//=> 'abc😀' | ||
``` | ||
StringView also offers methods for searching and in-place changing the underlying string without decoding: | ||
```javascript | ||
const stringView = StringView.fromString('abc😀a'); | ||
const searchValue = StringView.fromString('😀'); | ||
stringView.search(searchValue); // equivalent of String#indexOf | ||
//=> 3 | ||
const replacement = StringView.fromString('d'); | ||
stringView.replace(searchValue, replacement).toString(); | ||
//=> 'abcda' | ||
stringView.reverse().toString(); | ||
//=> 'adcba' | ||
``` | ||
## Documentation | ||
@@ -413,0 +461,0 @@ - [API documentation](https://github.com/zandaqo/structurae/blob/master/doc/API.md) |
86084
12.3%14
7.69%2309
14.65%461
11.62%