Comparing version 3.0.0-pre.8 to 3.0.0-pre.9
var List = require('can-list'); | ||
var QUnit = require('steal-qunit'); | ||
var Observation = require('can-observation'); | ||
var Map = require('can-map'); | ||
require("can-map-define"); | ||
@@ -140,2 +141,3 @@ | ||
}); | ||
test('Array accessor methods', 11, function () { | ||
@@ -176,2 +178,44 @@ var l = new List([ | ||
}); | ||
test('Concatenated list items Equal original', function() { | ||
var l = new List([ | ||
{ firstProp: "Some data" }, | ||
{ secondProp: "Next data" } | ||
]), | ||
concatenated = l.concat([ | ||
{ hello: "World" }, | ||
{ foo: "Bar" } | ||
]); | ||
ok(l[0] === concatenated[0], "They are Equal"); | ||
ok(l[1] === concatenated[1], "They are Equal"); | ||
}); | ||
test('Lists with maps concatenate properly', function() { | ||
var Person = Map.extend(); | ||
var People = List.extend({ | ||
Map: Person | ||
},{}); | ||
var Genius = Person.extend(); | ||
var Animal = Map.extend(); | ||
var me = new Person({ name: "John" }); | ||
var animal = new Animal({ name: "Tak" }); | ||
var genius = new Genius({ name: "Einstein" }); | ||
var hero = { name: "Ghandi" }; | ||
var people = new People([]); | ||
var specialPeople = new People([ | ||
genius, | ||
hero | ||
]); | ||
people = people.concat([me, animal, specialPeople], specialPeople, [1, 2], 3); | ||
ok(people.attr('length') === 8, "List length is right"); | ||
ok(people[0] === me, "Map in list === vars created before concat"); | ||
ok(people[1] instanceof Person, "Animal got serialized to Person"); | ||
}); | ||
test('splice removes items in IE (#562)', function () { | ||
@@ -183,3 +227,2 @@ var l = new List(['a']); | ||
test('reverse triggers add/remove events (#851)', function() { | ||
@@ -381,1 +424,23 @@ expect(6); | ||
}); | ||
test('forEach callback', function () { | ||
var list = new List([]), | ||
counter = 0; | ||
list.attr(9, 'foo'); | ||
list.forEach(function (element, index, list) { | ||
counter++; | ||
}); | ||
equal(counter, 1, 'Should not be invoked for uninitialized attr keys'); | ||
}); | ||
test('each callback', function () { | ||
var list = new List([]), | ||
counter = 0; | ||
list.attr(9, 'foo'); | ||
list.each(function (item, index) { | ||
counter++; | ||
}); | ||
equal(counter, 1, 'Should not be invoked for uninitialized attr keys'); | ||
}); |
@@ -18,3 +18,2 @@ /* jshint -W079 */ | ||
var each = require('can-util/js/each/each'); | ||
var types = require("can-util/js/types/types"); | ||
@@ -36,2 +35,13 @@ | ||
// Function that serializes the passed arg if | ||
// type does not match MapType of `this` list | ||
// then adds to args array | ||
var serializeNonTypes = function(MapType, arg, args) { | ||
if(arg && arg.serialize && !(arg instanceof MapType)) { | ||
args.push(new MapType(arg.serialize())); | ||
} else { | ||
args.push(arg); | ||
} | ||
}; | ||
/** | ||
@@ -221,3 +231,3 @@ * @add can.List | ||
* | ||
* `each` iterates through the Map, calling a function | ||
* `each` iterates through the List, calling a function | ||
* for each element. | ||
@@ -228,3 +238,4 @@ * | ||
* arguments, respectively, to the callback. If the callback returns false, | ||
* the loop will stop. | ||
* the loop will stop. The callback is not invoked for List elements that were | ||
* never initialized. | ||
* | ||
@@ -236,3 +247,3 @@ * @return {can.List} this List, for chaining | ||
* var i = 0; | ||
* new can.Map([1, 10, 100]).each(function(element, index) { | ||
* new can.List([1, 10, 100]).each(function(element, index) { | ||
* i += element; | ||
@@ -244,3 +255,3 @@ * }); | ||
* i = 0; | ||
* new can.Map([1, 10, 100]).each(function(element, index) { | ||
* new can.List([1, 10, 100]).each(function(element, index) { | ||
* i += element; | ||
@@ -779,8 +790,28 @@ * if(index >= 1) { | ||
*/ | ||
concat: function () { | ||
var args = []; | ||
each(makeArray(arguments), function (arg, i) { | ||
args[i] = arg instanceof List ? arg.serialize() : arg; | ||
concat: function() { | ||
var args = [], | ||
MapType = this.constructor.Map; | ||
// Go through each of the passed `arguments` and | ||
// see if it is list-like, an array, or something else | ||
each(arguments, function(arg) { | ||
if(types.isListLike(arg) || Array.isArray(arg)) { | ||
// If it is list-like we want convert to a JS array then | ||
// pass each item of the array to serializeNonTypes | ||
var arr = types.isListLike(arg) ? makeArray(arg) : arg; | ||
each(arr, function(innerArg) { | ||
serializeNonTypes(MapType, innerArg, args); | ||
}); | ||
} | ||
else { | ||
// If it is a Map, Object, or some primitive | ||
// just pass arg to serializeNonTypes | ||
serializeNonTypes(MapType, arg, args); | ||
} | ||
}); | ||
return new this.constructor(Array.prototype.concat.apply(this.serialize(), args)); | ||
// We will want to make `this` list into a JS array | ||
// as well (We know it should be list-like), then | ||
// concat with our passed in args, then pass it to | ||
// list constructor to make it back into a list | ||
return new this.constructor(Array.prototype.concat.apply(makeArray(this), args)); | ||
}, | ||
@@ -794,3 +825,4 @@ | ||
* The three parameters that _callback_ gets passed are _element_, the element at _index_, _index_ the | ||
* current element of the list, and _list_ the List the elements are coming from. | ||
* current element of the list, and _list_ the List the elements are coming from. _callback_ is | ||
* not invoked for List elements that were never initialized. | ||
* @param {Object} [thisArg] the object to use as `this` inside the callback | ||
@@ -813,3 +845,3 @@ * | ||
item = this.attr(i); | ||
if (cb.call(thisarg || item, item, i, this) === false) { | ||
if (item !== undefined && cb.call(thisarg || item, item, i, this) === false) { | ||
break; | ||
@@ -816,0 +848,0 @@ } |
@@ -7,2 +7,3 @@ @module {constructor} can-list | ||
@release 2.0 | ||
@package ../package.json | ||
@@ -26,3 +27,3 @@ @group can-list.prototype 0 Prototype | ||
@param {can.Deferred} deferred A deferred that resolves to an | ||
@param {can.Deferred} deferred A deferred that resolves to an | ||
array. When the deferred resolves, its values will be added to the list. | ||
@@ -37,3 +38,3 @@ | ||
`List` is used to observe changes to an Array. `List` extends `[can-map]`, so all the | ||
`List` is used to observe changes to an Array. `List` extends `[can-map]`, so all the | ||
ways that you're used to working with Maps also work here. | ||
@@ -46,5 +47,5 @@ | ||
hobbies.attr("length") //-> 2 | ||
hobbies.attr(0,"JavaScript") | ||
hobbies.attr() //-> ["JavaScript","Party Rocking"] | ||
@@ -100,7 +101,7 @@ | ||
list.bind('add', function() { console.log('An element was added.'); }); | ||
list.bind('remove', function() { | ||
console.log('An element was removed.'); | ||
list.bind('remove', function() { | ||
console.log('An element was removed.'); | ||
}); | ||
list.bind('length', function() { | ||
console.log('The length of the list changed.'); | ||
list.bind('length', function() { | ||
console.log('The length of the list changed.'); | ||
}); | ||
@@ -107,0 +108,0 @@ |
{ | ||
"name": "can-list", | ||
"version": "3.0.0-pre.8", | ||
"version": "3.0.0-pre.9", | ||
"description": "Observable lists", | ||
@@ -5,0 +5,0 @@ "homepage": "http://canjs.com", |
@@ -223,3 +223,4 @@ # can-list | ||
arguments, respectively, to the callback. If the callback returns false, | ||
the loop will stop. | ||
the loop will stop. The callback is not invoked for List elements that were | ||
never initialized. | ||
@@ -226,0 +227,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
71010
1351
274
1