Skip to content

Commit

Permalink
feat: first pass at @nodevu/opt
Browse files Browse the repository at this point in the history
Signed-off-by: Tierney Cyren <[email protected]>
  • Loading branch information
bnb committed Nov 8, 2024
1 parent ec70307 commit dcccd32
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 1 deletion.
21 changes: 21 additions & 0 deletions opt/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License Copyright (c) 2022 Tierney Cyren

Permission is hereby granted, free
of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
(including the next paragraph) shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
34 changes: 34 additions & 0 deletions opt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @nodevu/optionsparser

A tool that fetches the /dist/index.json file from the Node.js website.

## Usage

```js
const opt = require('@nodevu/opt')

const parsedOptions = await parser(); // returns a huge JSON object
```

```js
const opt = require('@nodevu/opt')

const options = {
fetch: globalThis.fetch, // use your own fetch if you want!
urls: {
index: 'https://nodejs.org/dist/index.json',
},
};

const parsedOptions = await parser(options); // returns a huge JSON object
```

## API
- `opt(options)`
- `options` (object): Options object.
- `fetch` (function): Fetch function. Default: `globalThis.fetch`.
- `urls` (object): URLs object.
- `index` (string): URL to fetch the index.json file from. Default: `'https://nodejs.org/dist/index.json'`.
- `schedule` (string): URL to fetch the schedule.json file from. Default: `'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json'`.
- `accept` (array): Array of strings that represent the accepted versions. Default: `['lts', 'current']`.
- Returns: Promise that resolves with the fetched `index.json` object.
17 changes: 17 additions & 0 deletions opt/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.1/schema.json",
"organizeImports": {
"enabled": true
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}
44 changes: 44 additions & 0 deletions opt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { DateTime } = require('luxon');

// this function allows us to parse user-passed options.
//
// this is particularly useful for tests so we can reduce variables
// and ensure that our test suite is able to be consistent.
function opt(inputOptions) {
// set up our defaults
const result = {
fetch: globalThis.fetch,
now: DateTime.now(),
urls: {
index: 'https://nodejs.org/dist/index.json',
schedule:
'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json',
},
};

if (typeof inputOptions === 'undefined') {
return result
}

// allow the end-user to replace our fetch implementation with another one they prefer.
if (inputOptions?.fetch) {
result.fetch = inputOptions.fetch;
}

// allow the end-user to provide a custom DateTime. This is particularly useful for tests.
if (inputOptions?.now) {
result.now = inputOptions.now;
}

if (inputOptions?.urls?.index) {
result.urls.index = inputOptions.urls.index;
}

if (inputOptions?.urls?.schedule) {
result.urls.schedule = inputOptions.urls.schedule;
}

return result;
}

module.exports = opt;
32 changes: 32 additions & 0 deletions opt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@nodevu/opt",
"version": "0.1.0",
"description": "internal options parser for @nodevu packages.",
"main": "index.js",
"files": ["index.js", "LICENSE"],
"scripts": {
"lint": "biome check ./",
"lint:write": "biome check ./ --write",
"test": "node --test",
"coverage": "c8 node --test",
"updates:check": "npx npm-check-updates",
"updates:update": "npx npm-check-updates -u"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cutenode/nodevu.git"
},
"keywords": ["node.js", "versions"],
"author": "Tierney Cyren <[email protected]> (https://bnb.im/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/cutenode/nodevu/issues"
},
"homepage": "https://github.com/cutenode/nodevu#readme",
"devDependencies": {
"@biomejs/biome": "1.9.4",
"c8": "^10.1.2",
"luxon": "^3.5.0",
"undici": "^6.20.1"
}
}
74 changes: 74 additions & 0 deletions opt/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { deepStrictEqual } = require('node:assert');
const { describe, it, beforeEach } = require('node:test');
const { fetch: undiciFetch } = require('undici');
const { DateTime } = require('luxon');
const nodevu = require('@nodevu/core');
const opt = require('../index');

describe('the parseOptions module should return all correct defaults', async () => {
it('should return the default date', async () => {
const now = DateTime.now();
const defaultParsedOptions = opt();
deepStrictEqual(defaultParsedOptions.now.day, now.day);
deepStrictEqual(defaultParsedOptions.now.hour, now.hour);
deepStrictEqual(defaultParsedOptions.now.minute, now.minute);
deepStrictEqual(defaultParsedOptions.now.month, now.month);
});

it('should return the default date, when options is an empty object', async () => {
const now = DateTime.now();
const defaultParsedOptions = opt({});
deepStrictEqual(defaultParsedOptions.now.day, now.day);
deepStrictEqual(defaultParsedOptions.now.hour, now.hour);
deepStrictEqual(defaultParsedOptions.now.minute, now.minute);
deepStrictEqual(defaultParsedOptions.now.month, now.month);
});

it('defaultParsedOptions.fetch should be globalThis.fetch when no options are passed', async () => {
const defaultParsedOptions = opt();
deepStrictEqual(defaultParsedOptions.fetch, globalThis.fetch);
});

it('should return the origin index.json for url.index', async () => {
const defaultParsedOptions = opt();
deepStrictEqual(
defaultParsedOptions.urls.index,
'https://nodejs.org/dist/index.json',
);
});

it('should return the origin schedule.json for url.schedule', async () => {
const defaultParsedOptions = opt();
deepStrictEqual(
defaultParsedOptions.urls.schedule,
'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json',
);
});
});

describe('the parseOptions module should still work when defaults are changed', async () => {
it('should still work when a custom date is passed', async () => {
const currentNow = DateTime.now();
const defaultParsedOptions = opt({ now: currentNow });
deepStrictEqual(defaultParsedOptions.now, currentNow);
});

it('defaultParsedOptions.fetch should be globalThis.fetch when no options are passed', async () => {
const defaultParsedOptions = opt({ fetch: undiciFetch });
deepStrictEqual(defaultParsedOptions.fetch, undiciFetch);
});

it('should return the origin index.json for url.index', async () => {
const defaultParsedOptions = opt({
urls: { index: 'https://example.com' },
});
deepStrictEqual(defaultParsedOptions.urls.index, 'https://example.com');
});

it('should return the origin schedule.json for url.schedule', async () => {
const defaultParsedOptions = opt({
urls: { schedule: 'https://example.com' },
});
deepStrictEqual(defaultParsedOptions.urls.schedule, 'https://example.com');
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"ranges",
"aliases",
"translate",
"fetchindex"
"fetchindex",
"opt"
]
}

0 comments on commit dcccd32

Please sign in to comment.