Skip to content

Commit

Permalink
fix issue with mapping different objects to same item
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlik committed May 13, 2021
1 parent f8e197e commit 7948798
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![CodeQL](https://github.com/pavlikm/to-item/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/pavlikm/to-item/actions/workflows/codeql-analysis.yml)

# to-item
map object to one item of given set
to-item is polyfil for mapping any javascript Object to one item of given set, which can be again any Object.

### Instalation
```npm install to-item```
Expand All @@ -10,25 +10,35 @@ map object to one item of given set
```npm run test```

### Usage
Basic usage for mapping any object (for example string) to one item of given set:
```javascript
require('to-item');

//basic usage
"Some String".to([10, 15, 20]); //15
```
Run A-B test. Based of given cookie run one of method:
```javascript
require('to-item');

//run AB test by cookie value
let funcA = function(){
console.log("function A");
}
let funcB = function(){
console.log("function B");
}
cookieValue.to([funcA, funcB])();
```
Get random IP from pool:
```javascript
require('to-item');

//get random ip from pool
let host = Math.random().to(['10.0.0.120', '10.0.0.121', '10.0.0.122']);
```
Get tip of the day:
```javascript
require('to-item');

//get Tip of the day
let tip = (new Date()).getDay().to(arrayOfDailyTips);

```
... and many other use cases ;)
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ Object.defineProperty(Object.prototype, 'to', {
return Math.abs(h)
}

function toString(obj){
let str = '';
for (const property in obj) {
if(obj.hasOwnProperty(property)){
str += `${property}: ${obj[property]}`;
}
}
return str;
}

if(typeof map === "undefined") return this.toString();
let int = toInt(this.toString());
let int = toInt(toString(this));
let nodes = Array.from(map).sort((a, b) => a - b);

return nodes[int % nodes.length];
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "to-item",
"version": "1.0.5",
"version": "1.0.6",
"description": "converts object to one item of given set",
"main": "index.js",
"scripts": {
Expand All @@ -12,9 +12,11 @@
},
"keywords": [
"string",
"array",
"object",
"convert",
"map",
"set",
"routing",
"hash"
],
Expand Down
16 changes: 6 additions & 10 deletions test/spec/test.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
class SomeObject {
constructor() {
}
to() {
return 'it works';
}
}
let obj = new SomeObject();
require('../../index');

describe('/convert to set', () => {
Expand Down Expand Up @@ -95,7 +87,12 @@ describe('/convert to set', () => {

describe('/convert object with "to" method', () => {
it('should preseve original "to" method', (done) => {
expect(obj.to(["a", "b"])).toEqual("it works");
let SomeObject = {
to: function () {
return 'it works';
}
}
expect(SomeObject.to(["a", "b"])).toEqual("it works");
done();
})
});
Expand All @@ -113,4 +110,3 @@ describe('/should use with functions', () => {
})
});


0 comments on commit 7948798

Please sign in to comment.