Skip to content

Commit

Permalink
added range function and bumped to v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
bohnacker committed May 22, 2023
1 parent 9d01086 commit d9d0161
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,5 @@ dist
# End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode

.idea

.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const gmynd = require("gmynd");
- [`polar(x, y)`](docs/polar.md) -> `object`
- [`circleRadius(area)`](docs/circleRadius.md) -> `number`
- [`circleArea(radius)`](docs/circleArea.md) -> `number`
- [`range(start, stop, [step])`](docs/range.md) -> `array`
- [`duration(date1, date2)`](docs/duration.md) -> `number`
- [`weekOfYear(date, [returnYear])`](docs/weekOfYear.md) -> `number` or `array`
- [`dayOfYear(date)`](docs/dayOfYear.md) -> `number`
Expand Down
21 changes: 21 additions & 0 deletions docs/range.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[Back to reference](../README.md)

### `range(stop)` or `range(start, stop, step=1)`

Implementation of the Python range function. Returns an array with numbers starting at `start` and going to the last possible value lower than `stop`.

if `start` is not supplied, it begins with `0`.

if `step` is not supplied, it defaults to `1`. `step` might also be negative, but then `start` must be higher than `stop`.

#### Examples:

```javascript
let myRange = gmynd.range(10);
// myRange will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

```javascript
let myRange = gmynd.range(5, 2, -0.5);
// myRange will be [5, 4.5, 4, 3.5, 3, 2.5]
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gmynd",
"version": "1.0.3",
"version": "1.0.4",
"description": "utility functions to help visualize JSON data",
"main": "index.js",
"directories": {
Expand Down
24 changes: 23 additions & 1 deletion src/gmynd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let gmynd = (function () {
return {

version: function () {
return "1.0.3";
return "1.0.4";
},

// JSON / Object manipulation related:
Expand Down Expand Up @@ -617,6 +617,28 @@ let gmynd = (function () {
};
},

range: function (start, stop, step = 1) {
// if stop is not given, range should go from 0 to start
if (!stop) {
[start, stop] = [0, start];
}

// From the Python docs:
// "If step is zero, ValueError is raised." -> Here, it returns just an empty array.
// "For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop."
// "For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop."
// "A range object will be empty if r[0] does not meet the value constraint."

if (step == 0) return [];
if (step > 0 && start >= stop) return [];
if (step < 0 && start <= stop) return [];

const size = Math.ceil((stop - start) / step);
if (size > 1000000) return [];
return [...Array(size).keys()].map(i => i * step + start);
},


// DATE FUNCTIONS
// ==============

Expand Down
11 changes: 10 additions & 1 deletion test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script src="./ourworldindata/world_ages.js" type="text/javascript"></script>
<script src="./ourworldindata/world_gender_ratio.js" type="text/javascript"></script>
<script src="./ourworldindata/country_locations.js" type="text/javascript"></script>
<script src="../dist/gmynd.js" type="text/javascript"></script>
<script src="../src/gmynd.js" type="text/javascript"></script>
</head>
<body>

Expand Down Expand Up @@ -193,6 +193,15 @@
console.log("dayOfYear()");
console.log(gmynd.dayOfYear(new Date("2021-05-17")));


console.log("--------------------------------------");
console.log("range(10)");
console.log(gmynd.range(10));
console.log("range(3, 5, 0.25)");
console.log(gmynd.range(3, 5, 0.25));
console.log("range(30, 17, -2)");
console.log(gmynd.range(30, 17, -2));

</script>

</body>
Expand Down

0 comments on commit d9d0161

Please sign in to comment.