Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit f52b0ba

Browse files
authored
feat(packages): ✨ Add snippets related to number (#60)
1 parent 31cb12c commit f52b0ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1249
-1
lines changed

.changeset/wicked-ravens-sell.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@terminal-nerds/snippets-number": minor
3+
---
4+
5+
✨ Add initial modules:
6+
7+
- `math`
8+
- `misc`
9+
- `random`
10+
- `round`
11+
- `schema`

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Reusable pieces of code, which can be **reused** across the projects.
100100
| ------------------------------- | ------------------------------------------------ |
101101
| [![snippets badge]][snippets] | [![snippets version badge]][snippets npm page] |
102102
| <!-- PACKAGES --> | <!-- PACKAGES --> |
103+
| [![number badge]][number] | [![number version badge]][number npm page] |
103104
| [![config badge]][config] | [![config version badge]][config npm page] |
104105
| [![extension badge]][extension] | [![extension version badge]][extension npm page] |
105106
| [![error badge]][error] | [![error version badge]][error npm page] |
@@ -116,6 +117,11 @@ Reusable pieces of code, which can be **reused** across the projects.
116117

117118
<!-- prettier-ignore-start -->
118119
<!-- PACKAGES LINKS -->
120+
[number]: ./packages/number/README.md
121+
[number badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-number&style=flat-square&color=informational
122+
[number version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-number/latest?style=flat-square&logo=npm
123+
[number npm page]: https://www.npmjs.com/package/@terminal-nerds/snippets-number
124+
119125
[config]: ./packages/config/README.md
120126
[config badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-config&style=flat-square&color=informational
121127
[config version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-config/latest?style=flat-square&logo=npm

packages/number/.depcheckrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignores": ["@terminal-nerds/*-config", "tsup", "vitest"]
3+
}

packages/number/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @terminal-nerds/snippets-number<!-- markdownlint-disable line-length list-marker-space no-duplicate-header ul-style ul-indent no-bare-urls -->

packages/number/LICENSE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/home/xeho91/Nextcloud/Projects/terminal-nerds/snippets/LICENSE.md

packages/number/README.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# @terminal-nerds/snippets-number
2+
3+
![package version badge]
4+
[![dependencies badge]][dependencies url]\
5+
[![install size badge]][install size url]
6+
7+
➡️ **This package wraps all available modules with snippets related to [Number]**
8+
— part of the [terminal-nerds/snippets] project.
9+
10+
[Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
11+
[terminal-nerds/snippets]: https://github.com/terminal-nerds/snippets
12+
[package version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-number/latest?style=for-the-badge&logo=npm
13+
[dependencies badge]: https://img.shields.io/librariesio/release/npm/@terminal-nerds/snippets-number?style=for-the-badge
14+
[dependencies url]: https://libraries.io/npm/@terminal-nerds%2snippets-number
15+
[install size badge]: https://packagephobia.com/badge?p=@terminal-nerds/snippets-number
16+
[install size url]: https://packagephobia.com/result?p=@terminal-nerds/snippets-number
17+
18+
## Modules included
19+
20+
[![documentation badge]][documentation url]
21+
22+
[documentation badge]: https://img.shields.io/static/v1?color=informational&style=for-the-badge&label=documentation&message=jsdocs.io
23+
[documentation url]: https://jsdocs.io/package/@terminal-nerds/snippets-number
24+
25+
<!-- prettier-sort-markdown-table -->
26+
27+
| Name | Size |
28+
| ------------------------------------------ | ----------------------------------------------------- |
29+
| [`@terminal-nerds/snippets-number/math`] | ![math size gzip badge] ![math size brotli badge] |
30+
| [`@terminal-nerds/snippets-number/misc`] | ![misc size gzip badge] ![misc size brotli badge] |
31+
| [`@terminal-nerds/snippets-number/random`] | ![random size gzip badge] ![random size brotli badge] |
32+
| [`@terminal-nerds/snippets-number/round`] | ![round size gzip badge] ![round size brotli badge] |
33+
| [`@terminal-nerds/snippets-number/schema`] | ![schema size gzip badge] ![schema size brotli badge] |
34+
35+
<!-- prettier-ignore-start -->
36+
<!-- MODULES LINKS -->
37+
[`@terminal-nerds/snippets-number/math`]: https://github.com/terminal-nerds/snippets/blob/main/packages/number/source/math/math.ts
38+
[math size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/math/math.js?label=gzip
39+
[math size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/math/math.js?label=brotli
40+
41+
[`@terminal-nerds/snippets-number/misc`]: https://github.com/terminal-nerds/snippets/blob/main/packages/number/source/misc/misc.ts
42+
[misc size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/misc/misc.js?label=gzip
43+
[misc size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/misc/misc.js?label=brotli
44+
45+
[`@terminal-nerds/snippets-number/random`]: https://github.com/terminal-nerds/snippets/blob/main/packages/number/source/random/random.ts
46+
[random size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/random/random.js?label=gzip
47+
[random size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/random/random.js?label=brotli
48+
49+
[`@terminal-nerds/snippets-number/round`]: https://github.com/terminal-nerds/snippets/blob/main/packages/number/source/round/round.ts
50+
[round size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/round/round.js?label=gzip
51+
[round size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/round/round.js?label=brotli
52+
53+
[`@terminal-nerds/snippets-number/schema`]: https://github.com/terminal-nerds/snippets/blob/main/packages/number/source/schema/schema.ts
54+
[schema size gzip badge]: https://badgen.net/badgesize/gzip/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/schema/schema.js?label=gzip
55+
[schema size brotli badge]: https://badgen.net/badgesize/brotli/file-url/unpkg.com/@terminal-nerds/snippets-number/dist/schema/schema.js?label=brotli
56+
<!-- prettier-ignore-end -->
57+
58+
---
59+
60+
## Prerequisites & usage
61+
62+
### Optional
63+
64+
[![supported typescript version badge]][typescript]
65+
66+
[typescript]: https://typescriptlang.org/
67+
[typescript icon]: https://api.iconify.design/logos/typescript-icon.svg
68+
[supported typescript version badge]: https://img.shields.io/github/package-json/dependency-version/terminal-nerds/snippets/peer/typescript?filename=packages%2Ftypescript%2Fpackage.json&logo=typescript&style=for-the-badge&label=typescript
69+
70+
If you are using ![typescript icon] [TypeScript],
71+
the latest version, which supports new features _(such as `satisfies`)_, is supported.
72+
73+
### Runtime environments
74+
75+
This package can be used in several runtime environments.
76+
We aim for cross-runtime compatibility and ensure proper error messages
77+
if a particular snippet cannot be run in the currently running environment.
78+
79+
#### Browsers
80+
81+
We use [browserslist] to define the minimum browsers versions supported.\
82+
Take a look at our [shared browserslist configuration] for more details.
83+
84+
[browserslist]: https://github.com/browserslist/browserslist
85+
[shared browserslist configuration]: https://github.com/terminal-nerds/configs/blob/main/packages/browserslist/source/browsers.ts
86+
87+
**Usage**:
88+
89+
```html
90+
<script type="module">
91+
import { snippet } from "https://cdn.jsdelivr.net/npm/@terminal-nerds/snippets-number";
92+
</script>
93+
```
94+
95+
---
96+
97+
#### Bun
98+
99+
We aim to support the latest version of ![bun icon] [bun].
100+
101+
**Usage**:
102+
103+
Firstly, install it:
104+
105+
```sh
106+
bun add @terminal-nerds/snippets-number
107+
```
108+
109+
And then in a particular file:
110+
111+
```js
112+
import { snippet } from "@terminal-nerds/snippets-number";
113+
```
114+
115+
[bun]: https://bun.sh/
116+
[bun icon]: https://api.iconify.design/logos/bun.svg
117+
118+
---
119+
120+
#### Deno
121+
122+
We aim to support the latest version of ![deno icon] [Deno].
123+
124+
**Usage**:
125+
126+
```ts
127+
import { snippet } from "npm:@terminal-nerds/snippets-number";
128+
```
129+
130+
[deno]: https://deno.land/
131+
[deno icon]: https://api.iconify.design/logos/deno.svg
132+
133+
---
134+
135+
#### Node.js
136+
137+
[![node.js version support badge]][node.js]
138+
139+
The latest ![node.js icon] [Node.js] LTS _(Long-Term Support)_ version is the minimum one supported.
140+
141+
> **Warning**\
142+
> **This package is written in [ES Module] _(ESM)_ type.**\
143+
> So, if you wish to use it in a project with CommonJS (CJS) type, you need to bundle this package or a particular module(s).
144+
145+
**Usage**:
146+
147+
Install it first with the Node.js package manager of your choice. In our example, we use [pnpm].
148+
149+
```sh
150+
pnpm add @terminal-nerds/snippets-number
151+
```
152+
153+
And then in a particular file:
154+
155+
```js
156+
import { snippet } from "@terminal-nerds/snippets-number";
157+
```
158+
159+
[ES Module]: https://www.freecodecamp.org/news/javascript-es-modules-and-module-bundlers
160+
[pnpm]: https://pnpm.io
161+
[node.js]: https://nodejs.org/en/
162+
[node.js icon]: https://api.iconify.design/logos/nodejs-icon.svg
163+
[node.js version support badge]: https://img.shields.io/node/v-lts/@terminal-nerds/snippets?style=for-the-badge&logo=nodedotjs
164+
165+
---
166+
167+
## Security
168+
169+
[![workflow security badge]][security policy]
170+
171+
🔐 For more information, please refer to the [Security section] at the root of
172+
the [terminal-nerds/snippets] monorepo.
173+
174+
[workflow security badge]: https://img.shields.io/github/actions/workflow/status/terminal-nerds/snippets/maintenance.yml?label=Security&logo=github&style=for-the-badge&branch=main
175+
[security section]: https://github.com/terminal-nerds/snippets#security
176+
[security policy]: https://github.com/terminal-nerds/snippets/security/policy
177+
178+
---
179+
180+
## License
181+
182+
[![license badge]][license]
183+
184+
⚖️ For more information, please refer to the [License section] at the root of the [terminal-nerds/snippets] monorepo.
185+
186+
[license]: https://github.com/terminal-nerds/snippets/blob/main/LICENSE.md
187+
[license badge]: https://img.shields.io/github/license/terminal-nerds/snippets?style=for-the-badge
188+
[license section]: https://github.com/terminal-nerds/snippets#License
189+
190+
### Contributing
191+
192+
[![contributors badge]][contributors url]
193+
194+
🤝 **Contributions of any kind are welcome!**
195+
196+
Please refer to the monorepo _([terminal-nerds/snippets])_ project's [CONTRIBUTING file] for more information
197+
if you wish to get involved.
198+
199+
[contributing file]: https://github.com/terminal-nerds/snippets/blob/main/.github/CONTRIBUTING.md
200+
[contributors badge]: https://img.shields.io/github/contributors/terminal-nerds/snippets?style=for-the-badge
201+
[contributors url]: https://github.com/terminal-nerds/snippets#contributors
202+
203+
### Author
204+
205+
🎉 The idea of this project was initiated by [xeho91]. However, it's the [contributors] who matter the most.
206+
207+
[contributors]: https://github.com/terminal-nerds/snippets/blob/main/README.md#project-contributors
208+
[xeho91]: https://github.com/xeho91

packages/number/package.json

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"$schema": "https://json.schemastore.org/package",
3+
"type": "module",
4+
"name": "@terminal-nerds/snippets-number",
5+
"version": "0.0.0",
6+
"description": "@terminal-nerds reusable snippets for numbers.",
7+
"keywords": [
8+
"snippets",
9+
"typescript",
10+
"number"
11+
],
12+
"license": "MIT",
13+
"author": {
14+
"name": "Mateusz Kadlubowski",
15+
"email": "[email protected]",
16+
"url": "https://xeho91.com"
17+
},
18+
"homepage": "https://github.com/terminal-nerds/snippets",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/terminal-nerds/snippets.git",
22+
"directory": "packages/number"
23+
},
24+
"bugs": "https://github.com/terminal-nerds/snippets/issues",
25+
"engines": {
26+
"node": ">=18"
27+
},
28+
"exports": {
29+
".": {
30+
"types": "./dist/main.d.ts",
31+
"import": "./dist/main.js",
32+
"require": "./dist/main.cjs"
33+
},
34+
"./*": {
35+
"types": "./dist/*/*.d.ts",
36+
"import": "./dist/*/*.js",
37+
"require": "./dist/*/*.cjs"
38+
},
39+
"./schema/*": {
40+
"types": "./dist/schema/groups/*/*.d.ts",
41+
"import": "./dist/schema/groups/*/*.js",
42+
"require": "./dist/schema/groups/*/*.cjs"
43+
}
44+
},
45+
"files": [
46+
"dist/"
47+
],
48+
"scripts": {
49+
"build": "tsup",
50+
"clean": "concurrently \"pnpm:clean:*\" --group --timings",
51+
"clean:build": "del \"./dist\"",
52+
"clean:cache": "del \"./node_modules/.cache\"",
53+
"clean:test": "del \"./coverage\"",
54+
"dev:test": "vitest watch --ui",
55+
"dev:pkg": "tsup --watch",
56+
"fix": "concurrently \"pnpm:fix:*\" --group --timings",
57+
"fix:format": "pretty-quick --branch main --pattern \"packages/number/**/*\" --verbose",
58+
"fix:js": "pnpm lint:js --fix",
59+
"fix:md": "pnpm lint:md --fix",
60+
"fix:pkg": "syncpack format ; syncpack fix-mismatches",
61+
"lint": "concurrently \"pnpm:lint:*\" --group --timings",
62+
"lint:deps": "depcheck",
63+
"lint:format": "pnpm fix:format --check",
64+
"lint:js": "DEBUG=\"eslint:cli-engine\" eslint . --ext .cjs,.js,.ts,.json,.yml --cache --cache-location \"./node_modules/.cache/eslint\"",
65+
"lint:md": "markdownlint . --config \"../../.markdownlint.json\" --dot --ignore \"./LICENSE.md\" --ignore-path \"../../.gitignore\"",
66+
"lint:pkg": "syncpack list-mismatches",
67+
"lint:types": "tsc --noEmit",
68+
"test:unit": "vitest run --dir \"./source\""
69+
},
70+
"peerDependencies": {
71+
"typescript": "4.9.5"
72+
},
73+
"devDependencies": {
74+
"@terminal-nerds/snippets-test": "workspace:*"
75+
},
76+
"peerDependenciesMeta": {
77+
"typescript": {
78+
"optional": true
79+
}
80+
},
81+
"dependencies": {
82+
"@terminal-nerds/snippets-runtime": "workspace:*",
83+
"@terminal-nerds/snippets-type": "workspace:*",
84+
"type-fest": "3.6.1",
85+
"zod": "3.21.4"
86+
}
87+
}

packages/number/source/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* MODULES */
2+
export * from "./math/math.ts";
3+
export * from "./misc/misc.ts";
4+
export * from "./random/random.ts";
5+
export * from "./round/round.ts";
6+
export * from "./schema/schema.ts";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
2+
import { describe, expect, it } from "vitest";
3+
import { ZodError } from "zod";
4+
5+
import { fibonacci } from "./math.ts";
6+
7+
const FIBONACCI_NUMBERS = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] as const;
8+
const INVALID_INPUTS = [0, -1, 1.5] as const;
9+
10+
describe("fibonacci(number)", () => {
11+
it(throws(ZodError).on(`invalid number inputs`).samples(INVALID_INPUTS), () => {
12+
for (const invalid of INVALID_INPUTS) {
13+
expect(() => fibonacci(invalid)).toThrowError(ZodError);
14+
}
15+
});
16+
17+
it(returns({ what: "Number", value: "<value>" }).on(`valid inputs - positive integers`), () => {
18+
for (const [index, expected] of FIBONACCI_NUMBERS.entries()) {
19+
expect(fibonacci(index + 1)).toBe(expected);
20+
}
21+
});
22+
});

packages/number/source/math/math.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { validatePositiveInteger } from "../schema/groups/integer/integer.ts";
2+
3+
/** @see {@link https://en.wikipedia.org/wiki/Fibonacci_number} Fibonacci number */
4+
export function fibonacci(number: number): number {
5+
validatePositiveInteger(number);
6+
7+
if (number === 1 || number === 2) {
8+
return 1;
9+
}
10+
11+
return fibonacci(number - 1) + fibonacci(number - 2);
12+
}

0 commit comments

Comments
 (0)