diff --git a/README.md b/README.md index d705249..5233988 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,13 @@ require('to-item'); "Some String".to([10, 15, 20]); //15 //run AB test by cookie value -let service = cookieValue.to([new ServiceA(), new ServiceB()]); -service.run(); +let funcA = function(){ + console.log("function A"); +} +let funcB = function(){ + console.log("function B"); +} +cookieValue.to([funcA, funcB])(); //get random ip from pool let host = Math.random().to(['10.0.0.120', '10.0.0.121', '10.0.0.122']); diff --git a/index.js b/index.js index 3f41adf..b18e24d 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,21 @@ -Object.defineProperty(Object.prototype, 'toInt', { - value: function (){ - let key = this.toString(); - if(parseInt(key).toString() === key) return Math.abs(parseInt(key)); - let h = 0, i = key.length; - while (i > 0) { - h = (h << 5) - h + key.charCodeAt(--i) | 0; - } - return Math.abs(h); - }, - enumerable: false -}); - Object.defineProperty(Object.prototype, 'to', { value: function (map){ + + function toInt(key){ + if(parseInt(key).toString() === key) return Math.abs(parseInt(key)); + let h = 0, i = key.length; + while (i > 0) { + h = (h << 5) - h + key.charCodeAt(--i) | 0; + } + return Math.abs(h) + } + if(typeof map === "undefined") return this.toString(); - let int = this.toString().toInt(); + let int = toInt(this.toString()); let nodes = Array.from(map).sort((a, b) => a - b); return nodes[int % nodes.length]; }, enumerable: false }); + diff --git a/package.json b/package.json index 4fa2df5..4a1c11c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "to-item", - "version": "1.0.4", + "version": "1.0.5", "description": "converts object to one item of given set", "main": "index.js", "scripts": { diff --git a/test/spec/test.spec.js b/test/spec/test.spec.js index c746135..e161820 100644 --- a/test/spec/test.spec.js +++ b/test/spec/test.spec.js @@ -8,50 +8,6 @@ class SomeObject { let obj = new SomeObject(); require('../../index'); -describe('/convert string to int', () => { - - it('should convert empty string', (done) => { - expect("".toInt()).toEqual(jasmine.any(Number)); - done(); - }); - - it('should convert number string', (done) => { - expect("123".toInt()).toEqual(jasmine.any(Number)); - done(); - }); - - it('should convert non utf-8 string', (done) => { - expect("ḞԍНǏ𝙅ƘԸⲘ".toInt()).toEqual(jasmine.any(Number)); - done(); - }); - - it('should different number for different strings', (done) => { - expect("foo".toInt()).not.toEqual("bar".toInt()); - done(); - }); - - it('should convert multiline string', (done) => { - expect(` - first line - second line - `.toInt()).toEqual(jasmine.any(Number)); - done(); - }); - - it('should convert object String', (done) => { - expect((String("test")).toInt()).toEqual(jasmine.any(Number)); - done(); - }); - -}); - -describe('/convert int to int', () => { - it('should return the same number', (done) => { - expect("123".toInt()).toEqual(123); - done(); - }) -}) - describe('/convert to set', () => { it('should convert string to one of array', (done) => { @@ -130,6 +86,11 @@ describe('/convert to set', () => { done(); }) + it('should return different values for different strings', (done) => { + expect("foo".to(["a", "b"])).not.toEqual("bar".to(["a", "b"])); + done(); + }); + }); describe('/convert object with "to" method', () => { @@ -139,4 +100,17 @@ describe('/convert object with "to" method', () => { }) }); +describe('/should use with functions', () => { + it('should call correct function', (done) => { + let func1 = function (){ + return "func1"; + } + let func2 = function (){ + return "func2"; + } + expect(["func1", "func2"]).toContain(Math.random().to([func1, func2])()); + done(); + }) +}); +