Skip to content

Commit

Permalink
v1.1.0: no objects in constructor; bugfixes; improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kormanowsky committed Jul 27, 2020
1 parent 29f583a commit a48a376
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,26 @@ let arr = list.toArray(); // Gives [0, 0, 0, 1, 1]
let predefinedList = new BitList([0, 0, 0, 1, 1, 0, 0])
let predefinedListFromNumber = new BitList(31);
```
## Working with objects
Bit list may work with objects so it is convenient to use it when you need to configure something. Imagine you have a list of cities and want to configure, which of them are available for delivery.
```javascript
// Create a list of keys (your cities here)
const Cities = ["Moscow", "London", "Paris", "Prague"];
// Tip: extend BitList to use your own keys
class CitiesBitList extends BitList {
setObject(object) {
return super.setObject(object, Cities);
}

toObject() {
return super.toObject(Cities);
}
}
// Then use your class for configuration
let citiesOfRussia = new CitiesBitList().setObject({Moscow: true});
// Convert to number
let citiesNumber = citiesOfRussia.toNumber();
// ...
// Later, convert it back to object
let citiesFromNumber = new CitiesBitList(citiesNumber).toObject();
```
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class BitList {
/**
* Constructor.
* @param {Array|Number|Object|undefined} initialValue Initial value may be a number, an array or an object.
* @param {Array|Number|undefined} initialValue Initial value may be a number or an array.
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
Expand All @@ -18,11 +18,6 @@ class BitList {
this.rawList = 0;
if (initialValue instanceof Array) {
this.setArray(initialValue);
} else if (
typeof initialValue === "object" &&
!(initialValue === null)
) {
this.setObject(initialValue);
} else if (typeof initialValue === "number") {
this.rawList = initialValue;
} else if (!(typeof initialValue === "undefined")) {
Expand Down Expand Up @@ -78,6 +73,16 @@ class BitList {
}
}

/**
* Copies given number to the bit list.
* @param {Number} array Number to copy from.
* @since 1.1.0
* @author Mikhail Kormanowsky
*/
setNumber(number){
this.rawList = number;
}

/**
* Copies values from given Array to the bit list.
* @param {Array} array Array to copy from.
Expand Down Expand Up @@ -149,7 +154,9 @@ class BitList {
result[key] = 0;
});
array.forEach((bit, index) => {
result[keys[index]] = bit;
if(index < keys.length){
result[keys[index]] = bit;
}
});
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-bit-list",
"version": "1.0.2",
"version": "1.1.0",
"description": "Store arrays of boolean values in numbers.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit a48a376

Please sign in to comment.