mirror of
https://bitbucket.org/bellord13/kit.git
synced 2026-11-13 19:06:11 +03:00
186 lines
4.9 KiB
PHP
186 lines
4.9 KiB
PHP
<script src="../assets/config/config.js"></script>
|
|
<script src="../assets/core/core.js"></script>
|
|
<script src="../assets/core/core.register.js"></script>
|
|
|
|
<?
|
|
print_r('by php collection: ');
|
|
$x = [];
|
|
$x[10] = 10;
|
|
$x[1] = 1;
|
|
print_r($x);
|
|
|
|
?>
|
|
|
|
|
|
<script>
|
|
|
|
o = {
|
|
1: 10,
|
|
2: 20,
|
|
keyAs: 'vAs'
|
|
};
|
|
a = [10,20,30];
|
|
a['as'] = 100;
|
|
|
|
(function () {
|
|
window.args = arguments;
|
|
})(1, 2, 3, 'as');
|
|
|
|
function assertSame(value, need, msg) {
|
|
if(value === need) {
|
|
console.log(true, msg);
|
|
return;
|
|
}
|
|
console.error(msg, value, need);
|
|
}
|
|
</script>
|
|
|
|
|
|
<h1>в консоле</h1>
|
|
<script>
|
|
|
|
kit.ready(['collection'], function () {
|
|
|
|
console.warn('ARRAY');
|
|
|
|
var arr = [];
|
|
arr[10] = 100;
|
|
var collection = new kit.Collection(arr);
|
|
assertSame(collection.count(), 1, 'Количество');
|
|
assertSame(collection.has(10), true, 'has (int) 10');
|
|
assertSame(collection.has('10'), true, 'has 10');
|
|
assertSame(collection.has('5'), false, 'has 5');
|
|
assertSame(collection.get(3, 2), 2, 'get');
|
|
assertSame(collection.get(10, 2), 100, 'get');
|
|
assertSame(collection.get(11), undefined, 'get');
|
|
|
|
|
|
|
|
console.warn('Object');
|
|
|
|
var ob = {
|
|
a: 1,
|
|
b: 2,
|
|
10: 3,
|
|
};
|
|
|
|
collection = new kit.Collection(ob);
|
|
assertSame(collection.count(), 3, 'Количество');
|
|
assertSame(collection.has(10), true, 'has');
|
|
assertSame(collection.has('10'), true, 'has');
|
|
assertSame(collection.has('5'), false, 'has');
|
|
assertSame(collection.get('a'), 1, 'get');
|
|
assertSame(collection.get(3, 2), 2, 'get');
|
|
assertSame(collection.get(10, 2), 3, 'get');
|
|
assertSame(collection.get(11), undefined, 'get');
|
|
assertSame(JSON.stringify(collection.set(5, 19).all()), JSON.stringify({
|
|
a: 1,
|
|
b: 2,
|
|
10: 3,
|
|
5: 19,
|
|
}), 'get all');
|
|
|
|
|
|
var testFunction = function (q, w, e, r, t, y) {
|
|
var c = new kit.Collection(arguments);
|
|
assertSame(c.count(), 8, 'arguments count');
|
|
assertSame(c.get(0), 1, 'arguments get');
|
|
assertSame(c.get(7), 8, 'arguments get');
|
|
assertSame(c.get(8, 80), 80, 'arguments get');
|
|
}
|
|
testFunction(1, 2, 3, 4, 5, 6, 7, 8);
|
|
|
|
|
|
|
|
|
|
collection = new kit.Collection([1, 2, 3, '', 0, NaN, undefined, null, false, true]);
|
|
assertSame(collection.filter().count(), 4, 'filter');
|
|
|
|
collection = new kit.Collection([4, 5, 6]);
|
|
assertSame(collection.every(function (item) {
|
|
return item > 2;
|
|
}), true, 'every');
|
|
assertSame(collection.every(function (item) {
|
|
return item > 5;
|
|
}), false, 'every');
|
|
assertSame(collection.every(function (item) {
|
|
return item < 1;
|
|
}), false, 'every');
|
|
|
|
|
|
collection = new kit.Collection([10, 20, 30]);
|
|
assertSame(collection.first(), 10, 'first');
|
|
assertSame(collection.first(function (item) {
|
|
return item > 15;
|
|
}), 20, 'first');
|
|
assertSame(collection.first(function (item) {
|
|
return item < 0;
|
|
}), undefined, 'first');
|
|
|
|
assertSame(collection.last(), 30, 'first');
|
|
assertSame(collection.last(function (item) {
|
|
return item < 25;
|
|
}), 20, 'first');
|
|
assertSame(collection.last(function (item) {
|
|
return item < 0;
|
|
}), undefined, 'first');
|
|
|
|
assertSame(collection.keys().count(), 3, 'keys');
|
|
|
|
xOb = {1: 1, 100: 100};
|
|
x = new kit.Collection(xOb);
|
|
x.push(200);
|
|
assertSame(x.last(), 200, 'object push last');
|
|
x.push(0);
|
|
assertSame(x.last(), 0, 'object push last');
|
|
x.push(undefined);
|
|
assertSame(x.last(), undefined, 'object push last');
|
|
x.push('window');
|
|
assertSame(x.last(), 'window', 'object push last');
|
|
assertSame(x.get(x.lastKey()), 'window', 'get lastKey');
|
|
assertSame(x.length, 6, 'length');
|
|
|
|
x = new kit.Collection([1, 100]);
|
|
x.push(200);
|
|
assertSame(x.last(), 200, 'array push last');
|
|
x.push(0);
|
|
assertSame(x.last(), 0, 'array push last');
|
|
assertSame(x.length, 4, 'length');
|
|
|
|
x = new kit.Collection({1: 1, 100: 100});
|
|
x.push(10);
|
|
x.set('as', 13);
|
|
assertSame(x.length, 4, 'length');
|
|
x.forget('as');
|
|
assertSame(x.length, 3, 'length after forget');
|
|
assertSame(x.get('as', 33), 33, 'get after forget');
|
|
|
|
xOb = {1: 10, 100: 101};
|
|
x = new kit.Collection(xOb);
|
|
assertSame(x.pull(100), 101, 'pull');
|
|
assertSame(x.length, 1, 'length after pull');
|
|
|
|
xOb = {1: 10, 100: 101};
|
|
x = new kit.Collection(xOb);
|
|
assertSame(x.pop(), 101, 'pop');
|
|
assertSame(x.length, 1, 'length after pop');
|
|
|
|
xOb = {1: 10, 100: 101};
|
|
x = new kit.Collection(xOb);
|
|
assertSame(x.shift(), 10, 'shift');
|
|
assertSame(x.length, 1, 'length after shift');
|
|
|
|
xOb = {a: 'a10', b: 'b11'};
|
|
x = new kit.Collection(xOb);
|
|
x.set(1, 10);
|
|
assertSame(x.shift(), 'a10', 'shift +');
|
|
assertSame(x.length, 2, 'length after shift');
|
|
assertSame(x.last(), 10, 'last after shift');
|
|
|
|
|
|
|
|
});
|
|
|
|
</script>
|
|
|