Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

basic-collection

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

basic-collection

A generic collection class to contain array-like data

  • 0.0.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

basic-collection

A generic collection class to contain array-like data.

Installation

$ npm install basic-collection

To port it to Browser or any other environment (not node), use a JS bundler (Browserify, Webpack, etc.).

Basic usage

import BasicCollection from "basic-collection";

let Collection = new BasicCollection;

Collection.set( "key_1", "value_1" );
Collection.set( "key_2", "value_2" );
Collection.set( 1234567, true );

Collection.get( "key_1" ); // value_1
Collection.get( "key_2" ); // value_2
Collection.get( "key_3", "default" ); // default

Collection.has( "key_1" ); // true
Collection.has( "key_2" ); // true
Collection.has( "key_3" ); // false

Collection.remove( "key_2" );
console.log( Collection.size ); // 2
console.log( Collection.keys ); // [ 'key_1', 1234567 ]
console.log( Collection.values ); // [ 'value_1', true ]
console.log( Collection.entries ); // [ [ 'key_1', 'value_1' ], [ '1234567', 'true' ] ]

Collection.each( ( key, value, map ) => {
    console.log( `Collection[${key}] = ${value}` );
} );

Collection.clear();
console.log( Collection.size ); // 0

Creating custom collections

import BasicCollection from "basic-collection";

class MyCollection {
    constructor( parameters ) {
        super( parameters );
    }

    normalize( key ) {
        return `transformed_${key}`;
    }
}

let Collection = new MyCollection;

Collection.set( "key_1", "value_1" );
Collection.set( "key_2", "value_2" );

console.log( Collection.keys );
// => [ 'transformed_key_1', "transformed_key_2" ]

API

MethodDescriptionAccessReturn
normalize( key )Normalizes data key.protectedstring
set( key, value )Sets an attribute for the current collection. If the attribute name already exists, its value will be overwritten.publicnull
get( key[, default] )Returns an attribute of the collection or a default value if the key does not exist.publicmixed
has( key )Checks if an attribute exists in the current collection.publicbool
remove( key )Removes an attribute from the current collection.publicnull
clear()Clears all values.publicnull
each( callback )Executes a callback for each element of this Collection.publicnull
sizeCounts parameters.publicint
entriesReturns a new Iterator object that contains an array of [key, value] for each element in the Collection in insertion order.publiciterable
keysReturns a new Iterator object that contains the keys for each element in the Collection in insertion order.publiciterable
valuesReturns a new Iterator object that contains the values for each element in the Collection in insertion order.publiciterable

Tests

$ npm test

Keywords

FAQs

Package last updated on 13 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc