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

arrayq

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

arrayq - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

lib/contains.ts

9

package.json
{
"name": "arrayq",
"version": "0.1.0",
"version": "0.1.1",
"description": "Array query methods for Node.js",
"main": "src/index.ts",
"main": "lib/prototype.ts",
"scripts": {

@@ -11,4 +11,5 @@ "prepublish": "npm run build && npm run test",

"compile": "npm run compile:ts",
"compile:ts": "tsc --declaration --outDir es6 ./src/index.ts ./src/prototype.ts",
"test": "mocha ./test"
"compile:ts": "tsc",
"test": "mocha ./test",
"watch": "nodemon -i ./es6/ -w ./ --exec \"npm run compile && npm run test\""
},

@@ -15,0 +16,0 @@ "repository": {

# node-arrayq
Array query methods for Node.js
## Usage
```shell
npm install --save arrayq
```
**TypeScript - Extending Array prototype**
```js
import 'arrayq';
[1, 2, 3, 4, 5].qIntersect([3, 4, 5, 6, 7]).qSame([3, 4, 5]) === true
```
**TypeScript - Import**
```js
import { intersect, same } from 'arrayq/lib';
same(intersect([1, 2, 3, 4, 5], [3, 4, 5, 6, 7]), [3, 4, 5]) === true
```
**ES6 - Extending Array prototype**
```js
require('arrayq/es6/prototype')
[1, 2, 3, 4, 5].qIntersect([3, 4, 5, 6, 7]).qSame([3, 4, 5]) === true
```
**ES6 - Require**
```js
const { same, intersect } = require('arrayq/es6');
same(intersect([1, 2, 3, 4, 5], [3, 4, 5, 6, 7]), [3, 4, 5]) === true
```
## Methods
**Contains**
Check whether the first array contains all items of the right.
`[1, 2, 3, 4, 5].qContains([1, 3, 5])` returns `true`
`[1, 2, 3, 4, 5].qContains([4, 5, 6])` returns `false`
**Empty**
Same as [].length === 0
`[].qEmpty()` returns `true`
`[1].qEmpty()` returns `false`
**First**
Returns first (matching) item.
`[1, 2, 3, 4, 5].qFirst()` returns `1`
`[1, 2, 3, 4, 5].qFirst(x => x%2 === 0)` returns `2`
**Last**
Returns last (matching) item.
`[1, 2, 3, 4, 5].qLast()` returns `5`
`[1, 2, 3, 4, 5].qLast(x => x%2 === 0)` returns `4`
**Intersect**
Returns intersection of items from both arrays.
`[1, 2, 3, 4, 5].qIntersect([3, 4, 5, 6, 7])` returns `[3, 4, 5]`
`[1, 2, 3].qIntersect([2, 4, 7], (l, r) => l === r/2)` returns `[1, 2]`
**Rotate**
Rotates items using the specified offset.
`[1, 2, 3, 4, 5].qRotate(1)` returns `[5, 1, 2, 3, 4]`
`[1, 2, 3, 4, 5].qRotate(-1)` returns `[2, 3, 4, 5, 1]`
**Same**
Checks equality between two arrays (order is important).
`[1, 2, 3].qSame([1, 2, 3])` returns `true`
`[1, 2, 3].qSame([1, 2])` returns `false`
`[1, 2, 3].qSame([2, 4, 6], (l, r) => l === r/2)` returns `true`

@@ -5,2 +5,14 @@ const assert = require('assert');

describe('es6', function() {
it('require', function() {
const { same } = require('../es6');
assert(same([1,2,3], [1,2,3]));
});
it('cherry pick', function() {
const same = require('../es6/same').default;
assert(same([1,2,3], [1,2,3]));
});
});
describe('qSame', function() {

@@ -95,1 +107,61 @@ it('should return true if items and order are the same', function() {

});
describe('qMapMany', function() {
it('should flatten arrays', function() {
assert.deepEqual([[1, 2], [3, 4]].qMapMany(), [1, 2, 3, 4]);
});
it('should return all items under property x', function() {
assert.deepEqual([
{ x: [1, 2] },
{ x: [3, 4, 5] },
{ x: [6, 7] },
{ x: [8] }
].qMapMany(item => item.x), [
1, 2, 3, 4, 5, 6, 7, 8
]);
});
});
describe('qRotate', function() {
it('rotate 1 backward', function() {
// TODO
assert.deepEqual([1,2,3,4].qRotate(-1), [2,3,4,1]);
});
it('rotate 1 forward', function() {
// TODO
assert.deepEqual([1,2,3,4].qRotate(1), [4,1,2,3]);
});
it('rotate 7 forward', function() {
// TODO
assert.deepEqual([1,2,3,4].qRotate(7), [2,3,4,1]);
});
it('rotate 7 backward', function() {
// TODO
assert.deepEqual([1,2,3,4].qRotate(-7), [4,1,2,3]);
});
});
describe('qDistinct', function() {
it('should remain empty', function() {
assert.deepEqual([].qDistinct(), []);
});
it('should have a single 4 left', function() {
assert.deepEqual([4,4,4,4].qDistinct(), [4]);
});
it('should have a 1, 2, 3 and 4 left', function() {
assert.deepEqual([1,2,2,3,4,4].qDistinct(), [1, 2, 3, 4]);
});
it('should have a 1a and 2d left', function() {
assert.deepEqual([
{ key: '1', value: 'a' },
{ key: '1', value: 'b' },
{ key: '1', value: 'c' },
{ key: '2', value: 'd' },
{ key: '2', value: 'e' },
{ key: '2', value: 'f' }
].qDistinct(item => item.key), [
{ key: '1', value: 'a' },
{ key: '2', value: 'd' }
]);
});
});
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": true,
"noImplicitAny": false,
"noImplicitUseStrict": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"sourceMap": true
},
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": true,
"noImplicitAny": false,
"noImplicitUseStrict": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"sourceMap": true,
"outDir": "es6"
},
"include": [
"./lib/**/*"
],
"compileOnSave": false,
"buildOnSave": false
}
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