find-replace
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,60 +0,59 @@ | ||
"use strict"; | ||
var t = require("typical"); | ||
var arrayify = require("array-back"); | ||
var testValue = require("test-value"); | ||
'use strict' | ||
var arrayify = require('array-back') | ||
var testValue = require('test-value') | ||
/** | ||
Find and replace items in an array. | ||
* Find and replace items in an array. | ||
* | ||
* @module find-replace | ||
* @example | ||
* > findReplace = require('find-replace') | ||
* | ||
* > findReplace([ 1, 2, 3], 2, 'two') | ||
* [ 1, 'two', 3 ] | ||
* | ||
* > findReplace([ 1, 2, 3], 2, [ 'two', 'zwei' ]) | ||
* [ 1, [ 'two', 'zwei' ], 3 ] | ||
* | ||
* > findReplace([ 1, 2, 3], 2, 'two', 'zwei') | ||
* [ 1, 'two', 'zwei', 3 ] | ||
*/ | ||
module.exports = findReplace | ||
@module find-replace | ||
@example | ||
> findReplace = require("find-replace"); | ||
/** | ||
* @param {array} - the input array | ||
* @param {valueTest} - a query to match the value you're looking for | ||
* @param [replaceWith] {...any} - optional replacement items | ||
* @returns {array} | ||
* @alias module:find-replace | ||
*/ | ||
function findReplace (array, valueTest) { | ||
var found = [] | ||
var replaceWiths = arrayify(arguments) | ||
replaceWiths.splice(0, 2) | ||
> findReplace([ 1, 2, 3], 2, "two") | ||
[ 1, 'two', 3 ] | ||
arrayify(array).forEach(function (value, index) { | ||
var expanded = [] | ||
replaceWiths.forEach(function (replaceWith) { | ||
if (typeof replaceWith === 'function') { | ||
expanded = expanded.concat(replaceWith(value)) | ||
} else { | ||
expanded.push(replaceWith) | ||
} | ||
}) | ||
> findReplace([ 1, 2, 3], 2, [ "two", "zwei" ]) | ||
[ 1, [ 'two', 'zwei' ], 3 ] | ||
if (testValue(value, valueTest)) { | ||
found.push({ | ||
index: index, | ||
replaceWithValue: expanded | ||
}) | ||
} | ||
}) | ||
> findReplace([ 1, 2, 3], 2, "two", "zwei") | ||
[ 1, 'two', 'zwei', 3 ] | ||
*/ | ||
module.exports = findReplace; | ||
found.reverse().forEach(function (item) { | ||
var spliceArgs = [ item.index, 1 ].concat(item.replaceWithValue) | ||
array.splice.apply(array, spliceArgs) | ||
}) | ||
/** | ||
@param {array} - the input array | ||
@param {valueTest} - a query to match the value you're looking for | ||
@param [...replaceWith] {any} - optional replacement items | ||
@returns {array} | ||
@alias module:find-replace | ||
*/ | ||
function findReplace(array, valueTest){ | ||
var found = []; | ||
var replaceWiths = arrayify(arguments); | ||
replaceWiths.splice(0, 2); | ||
arrayify(array).forEach(function(value, index){ | ||
var expanded = []; | ||
replaceWiths.forEach(function(replaceWith){ | ||
if (typeof replaceWith === "function"){ | ||
expanded = expanded.concat(replaceWith(value)); | ||
} else { | ||
expanded.push(replaceWith); | ||
} | ||
}); | ||
if (testValue(value, valueTest)){ | ||
found.push({ | ||
index: index, | ||
replaceWithValue: expanded | ||
}); | ||
} | ||
}); | ||
found.reverse().forEach(function(item){ | ||
var spliceArgs = [ item.index, 1 ].concat(item.replaceWithValue); | ||
array.splice.apply(array, spliceArgs); | ||
}); | ||
return array; | ||
return array | ||
} |
{ | ||
"name": "find-replace", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Find and replace items of an array", | ||
@@ -22,10 +22,9 @@ "repository": "https://github.com/75lb/find-replace.git", | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^1.1.1", | ||
"tape": "^4.0.0" | ||
"jsdoc-to-markdown": "^1.3.3", | ||
"tape": "^4.4.0" | ||
}, | ||
"dependencies": { | ||
"array-back": "^1.0.1", | ||
"test-value": "^1.0.0", | ||
"typical": "^2.2.0" | ||
"array-back": "^1.0.2", | ||
"test-value": "^1.0.1" | ||
} | ||
} |
[![view on npm](http://img.shields.io/npm/v/find-replace.svg)](https://www.npmjs.org/package/find-replace) | ||
[![npm module downloads per month](http://img.shields.io/npm/dm/find-replace.svg)](https://www.npmjs.org/package/find-replace) | ||
[![npm module downloads](http://img.shields.io/npm/dt/find-replace.svg)](https://www.npmjs.org/package/find-replace) | ||
[![Build Status](https://travis-ci.org/75lb/find-replace.svg?branch=master)](https://travis-ci.org/75lb/find-replace) | ||
@@ -12,11 +12,11 @@ [![Dependency Status](https://david-dm.org/75lb/find-replace.svg)](https://david-dm.org/75lb/find-replace) | ||
```js | ||
> findReplace = require("find-replace"); | ||
> findReplace = require('find-replace') | ||
> findReplace([ 1, 2, 3], 2, "two") | ||
> findReplace([ 1, 2, 3], 2, 'two') | ||
[ 1, 'two', 3 ] | ||
> findReplace([ 1, 2, 3], 2, [ "two", "zwei" ]) | ||
> findReplace([ 1, 2, 3], 2, [ 'two', 'zwei' ]) | ||
[ 1, [ 'two', 'zwei' ], 3 ] | ||
> findReplace([ 1, 2, 3], 2, "two", "zwei") | ||
> findReplace([ 1, 2, 3], 2, 'two', 'zwei') | ||
[ 1, 'two', 'zwei', 3 ] | ||
@@ -23,0 +23,0 @@ ``` |
@@ -1,51 +0,51 @@ | ||
var test = require("tape"); | ||
var findReplace = require("../"); | ||
var test = require('tape') | ||
var findReplace = require('../') | ||
function fixture(){ | ||
return [ 1, 2, 3, 4, 2 ]; | ||
function fixture () { | ||
return [ 1, 2, 3, 4, 2 ] | ||
} | ||
function argv(){ | ||
return [ "--one", "1", "-abc", "three" ]; | ||
function argv () { | ||
return [ '--one', '1', '-abc', 'three' ] | ||
} | ||
test("find primitive, replace with primitive", function(t){ | ||
t.deepEqual( | ||
findReplace(fixture(), 2, "two"), | ||
[ 1, "two", 3, 4, "two" ] | ||
); | ||
t.end(); | ||
}); | ||
test('find primitive, replace with primitive', function (t) { | ||
t.deepEqual( | ||
findReplace(fixture(), 2, 'two'), | ||
[ 1, 'two', 3, 4, 'two' ] | ||
) | ||
t.end() | ||
}) | ||
test("find primitive, replace with array", function(t){ | ||
t.deepEqual( | ||
findReplace(fixture(), 2, [ "two", "zwei" ]), | ||
[ 1, [ "two", "zwei" ], 3, 4, [ "two", "zwei" ] ] | ||
); | ||
t.end(); | ||
}); | ||
test('find primitive, replace with array', function (t) { | ||
t.deepEqual( | ||
findReplace(fixture(), 2, [ 'two', 'zwei' ]), | ||
[ 1, [ 'two', 'zwei' ], 3, 4, [ 'two', 'zwei' ] ] | ||
) | ||
t.end() | ||
}) | ||
test("find primitive, replace with several primitives", function(t){ | ||
t.deepEqual( | ||
findReplace(fixture(), 2, "two", "zwei"), | ||
[ 1, "two", "zwei", 3, 4, "two", "zwei" ] | ||
); | ||
t.end(); | ||
}); | ||
test('find primitive, replace with several primitives', function (t) { | ||
t.deepEqual( | ||
findReplace(fixture(), 2, 'two', 'zwei'), | ||
[ 1, 'two', 'zwei', 3, 4, 'two', 'zwei' ] | ||
) | ||
t.end() | ||
}) | ||
test("getopt example", function(t){ | ||
t.deepEqual( | ||
findReplace(argv(), /^-(\w{2,})$/, function(match){ | ||
return [ "-a", "-b", "-c" ]; | ||
}), | ||
[ "--one", "1", "-a", "-b", "-c", "three" ] | ||
); | ||
t.end(); | ||
}); | ||
test('getopt example', function (t) { | ||
t.deepEqual( | ||
findReplace(argv(), /^-(\w{2,})$/, function (match) { | ||
return [ '-a', '-b', '-c' ] | ||
}), | ||
[ '--one', '1', '-a', '-b', '-c', 'three' ] | ||
) | ||
t.end() | ||
}) | ||
test("getopt example", function(t){ | ||
t.deepEqual( | ||
findReplace(argv(), /^-(\w{2,})$/, "bread", "milk"), | ||
[ "--one", "1", "bread", "milk", "three" ] | ||
); | ||
t.end(); | ||
}); | ||
test('getopt example', function (t) { | ||
t.deepEqual( | ||
findReplace(argv(), /^-(\w{2,})$/, 'bread', 'milk'), | ||
[ '--one', '1', 'bread', 'milk', 'three' ] | ||
) | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
2
98
6337
8
- Removedtypical@^2.2.0
Updatedarray-back@^1.0.2
Updatedtest-value@^1.0.1