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

stac

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stac - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

2

package.json
{
"name": "stac",
"version": "0.0.3",
"version": "0.0.4",
"description": "Maintain a sorted stack of things.",

@@ -5,0 +5,0 @@ "main": "stac.js",

@@ -10,2 +10,136 @@ stac

Examples
--------
**Add arbitrary things to a stack**
```js
var createStac = require('stac')
, stack = createStac();
stack.add('A');
stack.add(23);
stack.add(['foo', 'bar']);
stack.first({my: 'Object'});
stack.last('The End');
console.log(stack.items());
// [ { my: 'Object' }, 'A', 23, [ 'foo', 'bar' ], 'The End']
```
**Add things to a stack with weights**
```js
var createStac = require('stac')
, stack = createStac();
stack.add(3, 'C');
stack.add(1, 'A');
stack.add(4, 'D');
stack.add(2, 'B');
stack.forEach(function (letter) {
console.log(letter);
});
// A
// B
// C
// D
```
**Advanced usage: custom sortBy and comparator**
```js
var createStac = require('stac')
var stack = createStac({
sortBy: function (item) {
return item.width * item.height;
},
comparator: function (a, b) {
if (a === b) return 0;
// Reverse sort
return a < b ? 1 : -1;
}
});
stack.add({color: 'red', width: 5, height: 4});
stack.add({color: 'green', width: 1, height: 2});
stack.add({color: 'blue', width: 8, height: 2});
console.log(stack.map(function (item) {
return item.color;
}));
// [ 'red', 'blue', 'green' ]
```
API
---
### createStac([options])
The only export for this module. Returns instances of Stac objects.
```js
var createStac = require('stac');
// Create a stac object.
var stack = createStac();
// Create a stac object with options.
var another = createStac({
sortBy: 'age'
});
```
### Methods
Methods of `Stac` objects.
**stack.add ( [val], item )**
Add an item to the stack. Optionally pass a value to sort by.
**stack.remove ( item )**
Remove an item from the stack.
**stack.first ( [val], item ) / stack.unshift ( [val], item )**
Add an item to the front of the stack. Items inserted this way will *always* be sorted
in front of items added with `add()` or `last()`. Within the *first* set, items
will be sorted normally.
**stack.last ( [val], item ) / stack.push ( [val], item )**
Add an item to the end of the stack. Items inserted this way will *always* be
sorted behind items added with `add()` or `first()`. Within the *last* set, items
will be sorted normally.
**stack.clone()**
Returns a copy of a stack.
*Note: items in the stack will still reference the originals.*
**stack.items() / stack.toJSON()**
Returns the sorted array of items in the stack.
**stack.forEach ( iterator, [thisArg] )**
Iterate over items in the stack (just like `Array.prototype.forEach`).
**stack.map ( func, [thisArg] )**
Returns a mapped representation of the stack (just like an `Array.prototype.map`).
**stack.pop() / stack.shift()**
Returns and removes the last/first item from the stack (similar to `Array.prototype.map`).
**stack.length**
Returns the current length of the stack.
- - -

@@ -12,0 +146,0 @@

@@ -10,8 +10,8 @@

this._stack = [];
this._prop = options.prop || 'weight';
this._sortBy = options.sortBy || 'weight';
this._defaultVal = options.defaultVal || 0;
this._comparator = options.comparator || function (a, b) {
if (a.val === b.val) return 0;
return a.val < b.val ? -1 : 1;
if (a === b) return 0;
return a < b ? -1 : 1;
};

@@ -24,6 +24,12 @@

Stac.prototype._getVal = function (obj) {
if (typeof obj[this._prop] !== 'undefined') {
return obj[this._prop];
Stac.prototype._getVal = function (item) {
if (typeof item.val !== 'undefined') {
return item.val;
}
if (typeof this._sortBy === 'function') {
return this._sortBy(item.obj);
}
if (typeof item.obj[this._sortBy] !== 'undefined') {
return item.obj[this._sortBy];
}
return this._defaultVal;

@@ -37,3 +43,3 @@ };

if ((a.first === b.first) && (a.last === b.last)) {
return self._comparator(a, b);
return self._comparator(self._getVal(a), self._getVal(b));
}

@@ -57,3 +63,3 @@ else if (a.first || b.last) {

var item = {
val: (typeof val !== 'undefined') ? val : this._getVal(obj),
val: val,
obj: obj

@@ -84,3 +90,3 @@ };

first: true,
val: (typeof val !== 'undefined') ? val : this._getVal(obj),
val: val,
obj: obj

@@ -101,3 +107,3 @@ };

last: true,
val: (typeof val !== 'undefined') ? val : this._getVal(obj),
val: val,
obj: obj

@@ -110,2 +116,13 @@ };

Stac.prototype.items = Stac.prototype.toJSON = function () {
this._sort();
return this._stack.map(function (item) {
return item.obj;
});
};
Stac.prototype.map = function (iterator, thisArg) {
return this.items().map(iterator, thisArg);
};
Stac.prototype.forEach = function (iterator, thisArg) {

@@ -136,2 +153,4 @@ this._sort();

module.exports = Stac;
module.exports = function createStac (options) {
return new Stac(options);
};

@@ -1,2 +0,2 @@

var Stac = require('../');
var createStac = require('../');

@@ -7,3 +7,3 @@ describe('basic test', function () {

beforeEach(function () {
stack = new Stac();
stack = createStac();
});

@@ -30,4 +30,3 @@

assert.equal(stack.shift(), 'one');
assert.equal(stack.pop(), 'three');
assert.deepEqual(stack.items(), ['one', 'two', 'three']);
});

@@ -79,2 +78,30 @@

it('forEach() - can loop over the items', function () {
var check = [];
stack.add(1);
stack.add(2);
stack.add(3);
stack.forEach(function (num) {
check.push(num);
});
assert.deepEqual(check, [1, 2, 3]);
});
it('map() - can map the items', function () {
var mapped;
stack.add(1);
stack.add(2);
stack.add(3);
mapped = stack.map(function (num) {
return num + 1;
});
assert.deepEqual(mapped, [2, 3, 4]);
});
it('can deal with objects in the stack', function () {

@@ -99,3 +126,3 @@ var brian = {

it('can work with the default property (weight)', function () {
it('can sort with the default property (weight)', function () {
stack.add({

@@ -119,17 +146,58 @@ name: 'Apple',

it('can loop over the items', function () {
var check = [];
it('can sort with a custom property', function () {
stack = createStac({
sortBy: 'age'
});
stack.add(1);
stack.add(2);
stack.add(3);
stack.add({name: 'Joe', age: 23});
stack.add({name: 'Gramps', age: 67});
stack.add({name: 'May', age: 34});
stack.add({name: 'Bobby', age: 12});
stack.forEach(function (num) {
check.push(num);
assert.equal(stack.pop().name, 'Gramps');
assert.equal(stack.shift().name, 'Bobby');
});
it('can sort with a custom sortBy', function () {
stack = createStac({
sortBy: function (item) {
return item.width * item.height;
}
});
assert.deepEqual(check, [1, 2, 3]);
stack.add({color: 'red', width: 5, height: 4});
stack.add({color: 'green', width: 1, height: 2});
stack.add({color: 'blue', width: 8, height: 2});
assert.equal(stack.pop().color, 'red');
assert.equal(stack.shift().color, 'green');
});
it('can sort with a custom comparator', function () {
stack = createStac({
sortBy: 'medal',
comparator: function (a, b) {
if (a === 'gold') {
return 1;
}
if (b == 'gold') {
return -1;
}
if (a === 'silver') {
return 1;
}
return 0;
}
});
stack.add({name: 'László Cseh', medal: 'bronze'});
stack.add({name: 'Michael Phelps', medal: 'gold'});
stack.add({name: 'Ryan Lochte', medal: 'silver'});
assert.equal(stack.pop().name, 'Michael Phelps');
assert.equal(stack.pop().name, 'Ryan Lochte');
});
});
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