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

array-segments

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-segments - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

run.js

2

package.json
{
"name": "array-segments",
"version": "1.0.0",
"version": "1.0.1",
"description": "Finds element segments matches in an array",

@@ -5,0 +5,0 @@ "engines": {

@@ -12,9 +12,12 @@

```js
const array_segments = require('array-segments');
let res
res = array_segments.match(
['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b'],
['a', 'b', 'c', 'd', 'e', 'f', 'a', 'z', 'a', 'b'],
[
['c', 'd', 'e'],
['b', 'c', 'd', 'e'],
['a', 'z'],
['a', 'b'],

@@ -24,25 +27,23 @@ ['a']

// Outputs
// [
// {segment: ['c', 'd', 'e'], indices: [2]},
// {segment: ['a', 'b'], indices: [0, 6]}
// ]
console.log(res)
// [ { segment: [ 'c', 'd', 'e' ], indices: [ 2 ] },
// { segment: [ 'a', 'z' ], indices: [ 6 ] },
// { segment: [ 'a', 'b' ], indices: [ 0, 8 ] } ]
// Note the upper case "C"
// Case insensitive match
res = array_segments.match(
['a', 'b', 'C', 'd', 'e', 'f', 'a', 'b'],
['a', 'b', 'c', 'D', 'E', 'f', 'a', 'z', 'a', 'b'],
[
['c', 'd', 'e'],
['C', 'd', 'E'],
['b', 'c', 'd', 'e'],
['a', 'z'],
['a', 'b'],
['a']
],
true
)
true)
// Outputs
// [
// {segment: ['c', 'd', 'e'], indices: [2]},
// {segment: ['a', 'b'], indices: [0, 6]}
// ]
console.log(res)
// [ { segment: [ 'c', 'd', 'e' ], indices: [ 2 ] },
// { segment: [ 'a', 'z' ], indices: [ 6 ] },
// { segment: [ 'a', 'b' ], indices: [ 0, 8 ] } ]
```

@@ -49,0 +50,0 @@

@@ -10,2 +10,3 @@ 'use strict'

// The indices of the used elements in the array
let used = []

@@ -19,30 +20,36 @@

}
let index = array.indexOf(segment[0])
// The indices of the segment in the array. That is for each segment,
// the index in the array of the first element of the segment.
const indices = []
const segment_used = []
while (index > -1) {
indices.push(index)
// The indices in the array of each segment elem
const indices_of_segment_elems = []
let res = segment.every((elem, idx) => {
let idx_global = idx + index
let i = 0
let res = segment.every((elem) => {
let idx_of_segment_elem = index + i
if (used.includes(idx_global)) {
if (used.includes(idx_of_segment_elem)) {
return false
}
segment_used.push(idx_global)
return elem == array[idx_global]
indices_of_segment_elems.push(idx_of_segment_elem)
i += 1
return elem == array[idx_of_segment_elem]
})
if (res) {
used = used.concat(segment_used)
indices.push(index)
used = used.concat(indices_of_segment_elems)
}
index = array.indexOf(segment[0], index + 1)
}
// When this is the last segment match, store in the segment matches
// in the global matches.
if (res && index < 0) {
matches.push({segment, indices})
}
if (indices.length) {
matches.push({segment, indices})
}

@@ -49,0 +56,0 @@

@@ -10,8 +10,9 @@ 'use strict'

it('works on case sensitive match', () => {
it('matches', () => {
const res = array_segments.match(
['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b'],
['a', 'b', 'c', 'd', 'e', 'f', 'a', 'z', 'a', 'b'],
[
['c', 'd', 'e'],
['b', 'c', 'd', 'e'],
['a', 'z'],
['a', 'b'],

@@ -22,21 +23,22 @@ ['a']

{segment: ['c', 'd', 'e'], indices: [2]},
{segment: ['a', 'b'], indices: [0, 6]}
{segment: ['a', 'z'], indices: [6]},
{segment: ['a', 'b'], indices: [0, 8]}
])
})
it('works on case insensitive match', () => {
it('matches case insensitively', () => {
const res = array_segments.match(
['a', 'b', 'C', 'd', 'e', 'f', 'a', 'b'],
['a', 'b', 'c', 'D', 'E', 'f', 'a', 'z', 'a', 'b'],
[
['c', 'd', 'e'],
['C', 'd', 'E'],
['b', 'c', 'd', 'e'],
['a', 'z'],
['a', 'b'],
['a']
],
true
)
true)
res.must.be.eql([
{segment: ['c', 'd', 'e'], indices: [2]},
{segment: ['a', 'b'], indices: [0, 6]}
{segment: ['a', 'z'], indices: [6]},
{segment: ['a', 'b'], indices: [0, 8]}
])

@@ -43,0 +45,0 @@ })

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