common-sequence
Advanced tools
| (function (factory) { | ||
| typeof define === 'function' && define.amd ? define(factory) : | ||
| factory(); | ||
| }(function () { 'use strict'; | ||
| /** | ||
| * Returns an array containing the initial elements which both input arrays have in common. | ||
| * | ||
| * A common use-case for this is discovering common ancestors between two file paths. | ||
| * | ||
| * ```js | ||
| * > commonSequence = require("common-sequence"); | ||
| * | ||
| * > pathA = "/Users/lloyd/Documents/75lb/dmd".split("/"); | ||
| * > pathB = "/Users/lloyd/Documents/75lb/array-tools".split("/"); | ||
| * | ||
| * > commonSequence(pathA, pathB).join("/"); | ||
| * '/Users/lloyd/Documents/75lb' | ||
| * ``` | ||
| * | ||
| * or a more trivial example: | ||
| * ```js | ||
| * > a.commonSequence([ 1, 2, 3 ], [ 1, 2, 4 ]) | ||
| * [ 1, 2 ] | ||
| * ``` | ||
| * @module common-sequence | ||
| */ | ||
| module.exports = commonSequence; | ||
| /** | ||
| * Returns the initial elements which both input arrays have in common | ||
| * @param {Array} - first array to compare | ||
| * @param {Array} - second array to compare | ||
| * @returns {Array} | ||
| * @alias module:common-sequence | ||
| */ | ||
| function commonSequence (a, b) { | ||
| var result = []; | ||
| for (var i = 0; i < Math.min(a.length, b.length); i++) { | ||
| if (a[i] === b[i]) { | ||
| result.push(a[i]); | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| return result | ||
| } | ||
| })); |
+42
| /** | ||
| * Returns an array containing the initial elements which both input arrays have in common. | ||
| * | ||
| * A common use-case for this is discovering common ancestors between two file paths. | ||
| * | ||
| * ```js | ||
| * > commonSequence = require("common-sequence"); | ||
| * | ||
| * > pathA = "/Users/lloyd/Documents/75lb/dmd".split("/"); | ||
| * > pathB = "/Users/lloyd/Documents/75lb/array-tools".split("/"); | ||
| * | ||
| * > commonSequence(pathA, pathB).join("/"); | ||
| * '/Users/lloyd/Documents/75lb' | ||
| * ``` | ||
| * | ||
| * or a more trivial example: | ||
| * ```js | ||
| * > a.commonSequence([ 1, 2, 3 ], [ 1, 2, 4 ]) | ||
| * [ 1, 2 ] | ||
| * ``` | ||
| * @module common-sequence | ||
| */ | ||
| module.exports = commonSequence | ||
| /** | ||
| * Returns the initial elements which both input arrays have in common | ||
| * @param {Array} - first array to compare | ||
| * @param {Array} - second array to compare | ||
| * @returns {Array} | ||
| * @alias module:common-sequence | ||
| */ | ||
| function commonSequence (a, b) { | ||
| var result = [] | ||
| for (var i = 0; i < Math.min(a.length, b.length); i++) { | ||
| if (a[i] === b[i]) { | ||
| result.push(a[i]) | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| return result | ||
| } |
+38
| [](https://www.npmjs.org/package/common-sequence) | ||
| [](https://www.npmjs.org/package/common-sequence) | ||
| [](https://travis-ci.org/75lb/common-sequence) | ||
| [](https://david-dm.org/75lb/common-sequence) | ||
| {{>main}} | ||
| ### Load anywhere | ||
| This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation. | ||
| Node.js: | ||
| ```js | ||
| const arrayify = require('common-sequence') | ||
| ``` | ||
| Within Node.js with ECMAScript Module support enabled: | ||
| ```js | ||
| import arrayify from 'common-sequence' | ||
| ``` | ||
| Within an modern browser ECMAScript Module: | ||
| ```js | ||
| import arrayify from './node_modules/common-sequence/index.mjs' | ||
| ``` | ||
| Old browser (adds `window.commonSequence`): | ||
| ```html | ||
| <script nomodule src="./node_modules/common-sequence/dist/index.js"></script> | ||
| ``` | ||
| * * * | ||
| © 2015-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
+1
-1
| The MIT License (MIT) | ||
| Copyright (c) 2015 Lloyd Brookes <75pound@gmail.com> | ||
| Copyright (c) 2015-19 Lloyd Brookes <75pound@gmail.com> | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
+16
-10
| { | ||
| "name": "common-sequence", | ||
| "author": "Lloyd Brookes <75pound@gmail.com>", | ||
| "version": "1.0.2", | ||
| "version": "2.0.0", | ||
| "description": "Returns an array containing the initial elements which both input arrays have in common", | ||
| "repository": "https://github.com/75lb/common-sequence.git", | ||
| "repository": "https://github.com/75lb/common-sequence", | ||
| "license": "MIT", | ||
| "main": "./lib/common-sequence.js", | ||
| "main": "dist/index.js", | ||
| "module": "index.mjs", | ||
| "keywords": [ | ||
@@ -20,13 +21,18 @@ "common", | ||
| "engines": { | ||
| "node": ">=0.10.0" | ||
| "node": ">=8" | ||
| }, | ||
| "scripts": { | ||
| "test": "tape test/*.js", | ||
| "lint": "jshint lib/*.js bin/*.js test/*.js; echo", | ||
| "docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
| "test": "npm run dist && test-runner test.js", | ||
| "docs": "jsdoc2md -c jsdoc.conf -t README.hbs index.mjs > README.md", | ||
| "dist": "rollup -f umd -n commonSequence -o dist/index.js index.mjs" | ||
| }, | ||
| "devDependencies": { | ||
| "jsdoc-to-markdown": "^1.1.1", | ||
| "tape": "^4.0.0" | ||
| } | ||
| "jsdoc-to-markdown": "^5.0.1", | ||
| "rollup": "^1.21.4", | ||
| "test-runner": "^0.6.0" | ||
| }, | ||
| "files": [ | ||
| "index.mjs", | ||
| "dist/index.js" | ||
| ] | ||
| } |
+34
-4
| [](https://www.npmjs.org/package/common-sequence) | ||
| [](https://www.npmjs.org/package/common-sequence) | ||
| [](https://www.npmjs.org/package/common-sequence) | ||
| [](https://travis-ci.org/75lb/common-sequence) | ||
| [](https://david-dm.org/75lb/common-sequence) | ||
| [](https://david-dm.org/75lb/common-sequence) | ||
| <a name="module_common-sequence"></a> | ||
| ## common-sequence | ||
| Returns an array containing the initial elements which both input arrays have in common. | ||
| A common use-case for this is discovering common ancestors between two file paths. | ||
| A common use-case for this is discovering common ancestors between two file paths. | ||
@@ -29,2 +30,3 @@ ```js | ||
| <a name="exp_module_common-sequence--commonSequence"></a> | ||
| ### commonSequence(a, b) ⇒ <code>Array</code> ⏏ | ||
@@ -41,4 +43,32 @@ Returns the initial elements which both input arrays have in common | ||
| ### Load anywhere | ||
| This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation. | ||
| Node.js: | ||
| ```js | ||
| const arrayify = require('common-sequence') | ||
| ``` | ||
| Within Node.js with ECMAScript Module support enabled: | ||
| ```js | ||
| import arrayify from 'common-sequence' | ||
| ``` | ||
| Within an modern browser ECMAScript Module: | ||
| ```js | ||
| import arrayify from './node_modules/common-sequence/index.mjs' | ||
| ``` | ||
| Old browser (adds `window.commonSequence`): | ||
| ```html | ||
| <script nomodule src="./node_modules/common-sequence/dist/index.js"></script> | ||
| ``` | ||
| * * * | ||
| © 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). | ||
| © 2015-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
-17
| { | ||
| "bitwise": true, | ||
| "camelcase": true, | ||
| "eqeqeq": true, | ||
| "globals": { "describe" : false, "it": false, "beforeEach": false }, | ||
| "globalstrict": false, | ||
| "indent": 4, | ||
| "laxbreak": true, | ||
| "maxparams": 3, | ||
| "multistr": true, | ||
| "newcap": true, | ||
| "node": true, | ||
| "quotmark": "double", | ||
| "trailing": true, | ||
| "undef": true, | ||
| "unused": true | ||
| } |
Sorry, the diff of this file is not supported yet
| language: node_js | ||
| node_js: | ||
| - '0.12' | ||
| - '0.11' | ||
| - '0.10' | ||
| - 'iojs' |
| [](https://www.npmjs.org/package/common-sequence) | ||
| [](https://www.npmjs.org/package/common-sequence) | ||
| [](https://travis-ci.org/75lb/common-sequence) | ||
| [](https://david-dm.org/75lb/common-sequence) | ||
| {{>main}} | ||
| * * * | ||
| © 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
| "use strict"; | ||
| /** | ||
| Returns an array containing the initial elements which both input arrays have in common. | ||
| A common use-case for this is discovering common ancestors between two file paths. | ||
| ```js | ||
| > commonSequence = require("common-sequence"); | ||
| > pathA = "/Users/lloyd/Documents/75lb/dmd".split("/"); | ||
| > pathB = "/Users/lloyd/Documents/75lb/array-tools".split("/"); | ||
| > commonSequence(pathA, pathB).join("/"); | ||
| '/Users/lloyd/Documents/75lb' | ||
| ``` | ||
| or a more trivial example: | ||
| ```js | ||
| > a.commonSequence([ 1, 2, 3 ], [ 1, 2, 4 ]) | ||
| [ 1, 2 ] | ||
| ``` | ||
| @module common-sequence | ||
| */ | ||
| module.exports = commonSequence; | ||
| /** | ||
| Returns the initial elements which both input arrays have in common | ||
| @param {Array} - first array to compare | ||
| @param {Array} - second array to compare | ||
| @returns {Array} | ||
| @alias module:common-sequence | ||
| */ | ||
| function commonSequence(a, b){ | ||
| var result = []; | ||
| for (var i = 0; i < Math.min(a.length, b.length); i++){ | ||
| if (a[i] === b[i]){ | ||
| result.push(a[i]); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| return result; | ||
| } |
-21
| "use strict"; | ||
| var test = require("tape"); | ||
| var commonSequence = require("../"); | ||
| test(".commonSequence()", function(t){ | ||
| var arr1 = [ 1,2,3,4 ]; | ||
| var arr2 = [ 1,2,4,5 ]; | ||
| var expected = [ 1, 2 ]; | ||
| var result = commonSequence(arr1, arr2); | ||
| t.deepEqual(result, expected); | ||
| t.end(); | ||
| }); | ||
| test(".commonSequence() 2", function(t){ | ||
| var arr1 = [ 1,2,3,4 ]; | ||
| var arr2 = [ 0,2,3,4 ]; | ||
| var expected = [ ]; | ||
| var result = commonSequence(arr1, arr2); | ||
| t.deepEqual(result, expected); | ||
| t.end(); | ||
| }); |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
7747
27.52%87
55.36%73
69.77%3
50%6
-33.33%