0.1.0 • Published 10 months ago

hybrid-array v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

hybrid-array

npm.io npm.io npm.io

An Array with Object benefits : enjoy data[id] as much as data[index] & data.find(item).

Getting started

Installation

From browser

<script type="module">
    import {
        createImmutableHybridArray,
        createLiteHybridArray,
        createHybridArray
    } from 'https://cdn.jsdelivr.net/npm/hybrid-array/mod.js';
</script>

From Deno

import {
    createImmutableHybridArray,
    createLiteHybridArray,
    createHybridArray
} from 'https://git.kaki87.net/KaKi87/hybrid-array/raw/branch/master/mod.js';

From Node

import {
    createImmutableHybridArray,
    createLiteHybridArray,
    createHybridArray
} from 'hybrid-array';

Usage

For static data

Create an "immutable" hybrid array :

const hybridArray = createImmutableHybridArray(
    data, // required array of objects containing the primary key
    primaryKey // 'id' by default
);

Access items :

console.log(hybridArray[index]);
console.log(hybridArray[id]); // statically pointing to the object containing the same ID
  • When an ID is duplicated, all instances of it are accessible by index, and only the last one is accessible by ID ;
  • When an ID is edited, the old ID will still point to the current item and the new ID will still point to nothing (or to its current item if it's a duplicate) ;
  • When an item is replaced by index, its current ID pointer will still point to it even if the new ID is different ;
  • When an item is replaced by ID, its current index pointer will still point to its current item.
  • When an item is added by index, it will have no ID pointer ;
  • When an item is added by ID, it will have no index pointer ;
  • When an item is deleted by index, its ID reference will still exist ;
  • When an item is deleted by ID, its index reference will still exist.

For dynamic but trusted and small data

Create a "lite" hybrid array :

const hybridArray = createLiteHybridArray(
    data, // optional array of objects containing the primary key
    primaryKey // 'id' by default
);

Add (more) items :

hybridArray.push(item);
hybridArray[index] = item;

Access items :

console.log(hybridArray[index]);
console.log(hybridArray[id]); // runs `hybridArray.find(item => item.id === id)` under the hood
  • When an ID is duplicated, either initially or by edit, all instances of it are accessible by index, and only the lowest-index one is accessible by ID ;
  • When an item is added by ID, it will have no index pointer ;
  • When an item is deleted by ID, its index pointer will remain.

For dynamic and untrusted or bug data

Create a "fully-featured" hybrid array :

const hybridArray = createHybridArray(
    data, // optional array of objects containing the primary key
    primaryKey // 'id' by default
);

Add (more) items :

hybridArray.push(item);
hybridArray[index] = item;
hybridArray[id] = item;

Access items :

console.log(hybridArray[index]);
console.log(hybridArray[id]); // uses a Map between ID & index under the hood

Delete items :

delete hybridArray[index]; // also removes the ID reference
delete hybridArray[id]; // also removes the index reference
  • When an ID is duplicated, either initially or by edit, only the last instance of it is accessible by index or ID, the previous ones are overwritten.

Related projects

Detailed feature comparison

  • index & id are existing ones
  • nIndex & nId are new ones
  • eIndex & eId are existing ones but different from index & id
  • neIndex & neId are new or existing ones (but different from index & id)
  • { id } can contain any additional key/value pairs
  • {} contains any key/value pairs except id
ObjectArraycreateImmutableHybridArraycreateLiteHybridArraycreateHybridArrayjunosuarez/indexed-arrayzeke/keyed-array
▼ Read operations
data[index]NoYesYesYesYesYesYes
data[id]YesNoYesYesYesYesYes
data.lengthNoYesYesYesYesYesYes
JSON.stringify(data)YesYesYesYesYesYesYes
data[index].id = nIdNoYesNoYesYesNoNo
▼ Write operations
data[index].id = eIdNoNoNoNoYesNoNo
data[id].id = nIdNoNoNoYesYesNoNo
data[id].id = eIdNoNoNoNoYesNoNo
data[neIndex] = { id: nId }NoYesNoYesYesNoNo
data[index] = { id }NoNoNoNoYesNoNo
data[neId] = {}YesNoNoNoYesNoNo
data.push({ id: nId })NoYesNoYesYesNoNo
data.push({ id })NoNoNoNoYesNoNo
delete data[index]NoYesNoYesYesNoNo
delete data[id]YesNoNoNoYesNoNo
▼ Transforming write ops.
data.splice()NoYesNoYesYesNoNo
data.sort()NoYesYesYesYesYesYes
▼ Misc
Input controlNoNoN/ANoYesNoNo
Unique primary key constraintYesNoYesNoYesNoNo
Custom primary keyN/AN/AYesYesYesYesNo
0.1.0

10 months ago

0.1.0-dev.0

10 months ago