Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

adds fromArray #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var person = {
locationLens = objectLens('location'),
numberLens = objectLens('number'),
store = locationLens.andThen(numberLens).run(person);
// or
// store = Lens.fromArray(['location', 'number'])

console.log(store.get());
// 1006
Expand Down Expand Up @@ -50,6 +52,8 @@ var data = [{
configLens = objectLens('config'),
typeLens = objectLens('type'),
configTypeLens = configLens.andThen(typeLens);
// or
// configTypeLens = Lens.fromArray(['config', 'type'])

console.log(data.filter(function(o) {
return configTypeLens.run(o).fold(
Expand Down
8 changes: 8 additions & 0 deletions src/lens.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ Lens.arrayLens = function(index) {
});
};

Lens.fromArray = function(props) {
return props.reduce((prev, next) =>
prev.andThen(
typeof next === 'string' ? Lens.objectLens(next) : Lens.arrayLens(next)
), Lens.id()
);
};

module.exports = Lens;
8 changes: 8 additions & 0 deletions src/partial-lens.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ PartialLens.arrayLens = function(index) {
});
};

PartialLens.fromArray = function(props) {
return props.reduce((prev, next) =>
prev.andThen(
typeof next === 'string'? PartialLens.objectLens(next) : PartialLens.arrayLens(next)
), PartialLens.id()
);
};

module.exports = PartialLens;
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ exports.testUpdatePersonFirstSkill = function(test) {
test.done();
};

exports.testUpdatePersonFirstSkillFromArray = function(test) {
var store = Lens.fromArray(['skills', 0]).run(person);

test.equal(store.get(), 'JavaScript');
test.deepEqual(
store.set('Haskell'),
{
name: 'Brian McKenna',
skills: [
'Haskell',
'Scala'
],
location: {
number: 1006,
street: 'Pearl St',
postcode: 80302
}
}
);
test.done();
};


exports.testNestedFilter = function(test) {
var config = PartialLens.objectLens('config'),
type = PartialLens.objectLens('type'),
Expand Down Expand Up @@ -107,6 +130,33 @@ exports.testNestedFilter = function(test) {
test.done();
};

exports.testNestedFilterFromArray = function(test) {
var configType = PartialLens.fromArray(['config', 'type']),
result = data.filter(function(d) {
return configType.run(d).fold(
function(store) {
return store.get() == 2;
},
function() {
return false;
}
);
});

test.deepEqual(
result,
[
{
name: 'First record',
config: {
type: 2
}
}
]
);
test.done();
};

exports.testValidation = function(test) {
var Option = require('fantasy-options'),
o = PartialLens.objectLens,
Expand Down