Skip to content

Commit

Permalink
update readme and tests, removed toInt() method
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlik committed May 12, 2021
1 parent 03a7a49 commit f8e197e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 61 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
});

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
62 changes: 18 additions & 44 deletions test/spec/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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();
})
});


0 comments on commit f8e197e

Please sign in to comment.