Browse Source

[infinity] es6 proxies!

master
Muthu Kumar 6 years ago
parent
commit
7e15d0dded
  1. 2
      es6/index.js
  2. 24
      index.js
  3. 4
      spec/spec.js

2
es6/index.js

@ -1 +1 @@
module.exports = require('..');
module.exports = require('../index');

24
index.js

@ -127,6 +127,30 @@ class InfiniteList {
};
};
}
if(typeof Proxy !== 'undefined')
return new Proxy(this, {
get: (obj, key) => {
if(key in obj) return obj[key];
const index = (
typeof key === 'string' && /^\d*$/g.test(key)
) ? parseInt(key) : undefined;
if(index) return obj['get'](index);
},
has: (obj, key) => {
const index = (
typeof key === 'string' && /^\d*$/g.test(key)
) ? parseInt(key) : undefined;
return (
(key in obj) ||
(areNumbers(index) &&
(index % 1 === 0) &&
(index >= 0))
)
},
enumerate: obj => obj.keys(),
ownKeys: obj => obj.keys(),
})
}
}

4
spec/spec.js

@ -1,6 +1,6 @@
'use strict';
const InfiniteList = require('../es5/infinity.min');
const InfiniteList = require('../es6');
const {
InfiniteListItem
@ -40,7 +40,7 @@ describe("InfiniteList", () => {
it("Should be true", () => {
const Infinite = new InfiniteList(0, x => x + 2);
expect(Infinite.get(5) instanceof InfiniteListItem).toBe(true);
expect((Infinite.get(5)) instanceof InfiniteListItem).toBe(true);
});
it("Should be 22", () => {

Loading…
Cancel
Save