array-walker
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -7,3 +7,3 @@ (() => { | ||
function arrayWalker(items, observationCallback, ...lineage) { | ||
function arrayWalker(items, observationCallback, context, ...lineage) { | ||
@@ -14,7 +14,7 @@ items.forEach((value, key) => { | ||
arrayWalker(value, observationCallback, ...lineage, key); | ||
arrayWalker(value, observationCallback, context, ...lineage, key); | ||
} else { | ||
observationCallback(value, ...lineage, key); | ||
observationCallback.call(context, value, ...lineage, key); | ||
@@ -21,0 +21,0 @@ } |
{ | ||
"name": "array-walker", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Multi-dimensional array walker with observation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,2 +31,11 @@ # Array Walker | ||
// With context | ||
walker(['a', 'b'], callbackWithContext, {example: "The Answer"}); | ||
// The Answer, a, 0 | ||
// The Answer, b, 1 | ||
function callbackWithContext(value, key) { | ||
console.log(this.example, value, key); | ||
} | ||
``` | ||
@@ -41,3 +50,3 @@ ## Installation | ||
``` | ||
### walker(items, observationCallback, ...lineage) | ||
### walker(items, observationCallback, context, ...lineage) | ||
| Type | Data Type | Name | Description | | ||
@@ -47,2 +56,3 @@ | --- | --- | --- | --- | | ||
| parameter | function | observationCallback | The function to call when a non-array value is found. | | ||
| parameter | * | The context passed to the callback | | ||
| parameter | ...number | [lineage] | The parent indexes. | | ||
@@ -57,2 +67,3 @@ | returns | undefined | n/a | n/a | | ||
| --- | --- | --- | --- | | ||
| this | * | this | The context passed to the walker | | ||
| parameter | !\*[] | value | The value that was discovered. | | ||
@@ -59,0 +70,0 @@ | parameter | ...number | lineage | The indexes in each dimension of the array. | |
@@ -150,5 +150,6 @@ /* global describe it expect jasmine */ | ||
let items = ['t'], | ||
callback = jasmine.createSpy('callback'); | ||
callback = jasmine.createSpy('callback'), | ||
context; | ||
target(items, callback, 1, 2, 'three'); | ||
target(items, callback, context, 1, 2, 'three'); | ||
@@ -164,4 +165,22 @@ it('executes callback for each item', () => { | ||
describe('callbacks with context', () => { | ||
let items = ['t'], | ||
callback = jasmine.createSpy('callback'), | ||
context = { | ||
testing: 'the context' | ||
}; | ||
target(items, callback, context); | ||
it('provides context', () => { | ||
expect(callback.calls[0].object).toBe(context); | ||
}); | ||
}); | ||
}); | ||
})(); |
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
10017
131
71