basic-collection
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name" : "basic-collection", | ||
"version" : "0.1.0", | ||
"version" : "0.1.1", | ||
"keywords" : [ "map", "data", "collection" ], | ||
@@ -5,0 +5,0 @@ "description" : "A generic collection class to contain array-like data", |
139
README.md
@@ -5,5 +5,29 @@ # basic-collection | ||
## Table of contents: | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Example 1: Basic usage](#example-1-basic-usage) | ||
- [Example 2: Creating custom collections](#example-2-creating-custom-collections) | ||
- [Example 3: Filtering values](#example-3-filtering) | ||
- [API](#api) | ||
- [new BasicCollection( parameters )](#new-basiccollection-parameters-) | ||
- [.normalize( key )](#normalize-key-) | ||
- [.set( key, value )](#set-key-value-) | ||
- [.get( key[, default] )](#get-key-default-) | ||
- [.has( key )](#has-key-) | ||
- [.remove( key )](#remove-key-) | ||
- [.clear()](#clear) | ||
- [.each( callback )](#each-callback-) | ||
- [.filterByKey( pattern )](#filterbykey-pattern-) | ||
- [.filterByValue( pattern )](#filterbyvalue-pattern-) | ||
- [.size](#size) | ||
- [.entries](#entries) | ||
- [.keys](#keys) | ||
- [.values](#values) | ||
- [Tests](#tests) | ||
## Installation | ||
``` | ||
```bash | ||
$ npm install basic-collection | ||
@@ -14,4 +38,6 @@ ``` | ||
## Basic usage | ||
## Usage | ||
### *Example 1: Basic usage* | ||
```javascript | ||
@@ -48,3 +74,3 @@ import BasicCollection from "basic-collection"; | ||
## Creating custom collections | ||
### *Example 2: Creating custom collections* | ||
@@ -70,23 +96,100 @@ ```javascript | ||
console.log( Collection.keys ); | ||
// => [ 'transformed_key_1', "transformed_key_2" ] | ||
// => [ 'transformed_key_1', 'transformed_key_2' ] | ||
``` | ||
### *Example 3: Filtering values* | ||
You can filter data using patterns. A pattern can contain wildcards. There are predefined wildcards by a basic collection: | ||
``` | ||
[digit] // Match /([0-9]+)/ | ||
[alnum] // Match /([0-9A-Za-z]+)/ | ||
[alpah] // Match /([A-Za-z]+)/ | ||
[xdigit] // Match /([0-9A-Fa-f]+)/ | ||
[punct] // Match /([\p{P}\d]+)/ | ||
[print] // Match /([\x20-\x7e]*)/ | ||
[upper] // Match /([A-Z]+)/ | ||
[lower] // Match /([a-z]+)/ | ||
[all] // Match /(.*?)/ | ||
``` | ||
```javascript | ||
import BasicCollection from "basic-collection"; | ||
let Collection = new BasicCollection; | ||
Collection.set( "key_1", "value_1" ); | ||
Collection.set( "key_2", "value_2" ); | ||
Collection.set( "test1", "1_abc" ); | ||
Collection.set( "test2", "2_def" ); | ||
Collection.filterByKey( "key_[digit]" ); | ||
// => [ 'key_1', 'key_2' ] | ||
Collection.filterByValue( "[digit]_[lower]" ); | ||
// => [ '1_abc', '2_def' ] | ||
``` | ||
### API | ||
| Method | Description | Access | Return | | ||
|:------------------------|:------------|:-----------|:-----------| | ||
| `normalize( key )` | Normalizes data key. | protected | string | | ||
| `set( key, value )` | Sets an attribute for the current collection. If the attribute name already exists, its value will be overwritten. | public | null | | ||
| `get( key[, default] )` | Returns an attribute of the collection or a default value if the key does not exist. | public | mixed | | ||
| `has( key )` | Checks if an attribute exists in the current collection. | public | bool | | ||
| `remove( key )` | Removes an attribute from the current collection. | public | null | | ||
| `clear()` | Clears all values. | public | null | | ||
| `each( callback )` | Executes a callback for each element of this Collection. | public | null | | ||
| `size` | Counts parameters. | public | int | | ||
| `entries` | Returns a new Iterator object that contains an array of [key, value] for each element in the Collection in insertion order. | public | iterable | | ||
| `keys` | Returns a new Iterator object that contains the keys for each element in the Collection in insertion order. | public | iterable | | ||
| `values` | Returns a new Iterator object that contains the values for each element in the Collection in insertion order. | public | iterable | | ||
#### new BasicCollection( parameters ) | ||
Creates a new `BasicCollection` instance with custom parameters in ` [ [ 'key1', 'value1' ], [ 'key2', 'value2' ] ]` fotmat. | ||
#### .normalize( key ) | ||
Normalizes data key. | ||
#### .set( key, value ) | ||
Sets an attribute for the current collection. If the attribute name already exists, its value will be overwritten. | ||
#### .get( key[, default] ) | ||
Returns an attribute of the collection or a default value if the key does not exist. | ||
#### .has( key ) | ||
Checks if an attribute exists in the current collection. | ||
#### .remove( key ) | ||
Removes an attribute from the current collection. | ||
#### .clear() | ||
Clears all values. | ||
#### .each( callback ) | ||
Executes a callback for each element of this Collection. | ||
#### .filterByKey( pattern ) | ||
Returns keys that match a pattern. | ||
#### .filterByValue( pattern ) | ||
Returns values that match a pattern. | ||
#### .size | ||
Counts parameters. | ||
#### .entries | ||
Returns a new Iterator object that contains an array of `[key, value]` for each element in the Collection in insertion order. | ||
#### .keys | ||
Returns a new Iterator object that contains the keys for each element in the Collection in insertion order. | ||
#### .values | ||
Returns a new Iterator object that contains the values for each element in the Collection in insertion order. | ||
## Tests | ||
`$ npm test` | ||
```bash | ||
$ npm test | ||
``` |
@@ -118,4 +118,4 @@ "use strict"; | ||
filterByKey( pattern ) { | ||
let expression = filter.replace( pattern ); | ||
let filtered = []; | ||
const expression = filter.replace( pattern ); | ||
const filtered = []; | ||
@@ -132,3 +132,3 @@ for ( let key of this.keys ) { | ||
/** | ||
* Returns valyes that match a pattern. | ||
* Returns values that match a pattern. | ||
* | ||
@@ -140,4 +140,4 @@ * @param string pattern | ||
filterByValue( pattern ) { | ||
let expression = filter.replace( pattern ); | ||
let filtered = []; | ||
const expression = filter.replace( pattern ); | ||
const filtered = []; | ||
@@ -144,0 +144,0 @@ for ( let key of this.values ) { |
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
16011
192