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

Commit a44081b

Browse files
authored
feat(packages): ✨ Add new snippets related to object (#72)
1 parent 2cb9e45 commit a44081b

File tree

20 files changed

+476
-0
lines changed

20 files changed

+476
-0
lines changed

.changeset/curvy-pugs-know.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@terminal-nerds/snippets-object": minor
3+
---
4+
5+
Added a new modules with snippets:
6+
7+
- `keys`
8+
- `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+
| [![object badge]][object] | [![object version badge]][object npm page] |
103104
| [![url badge]][url] | [![url version badge]][url npm page] |
104105
| [![array badge]][array] | [![array version badge]][array npm page] |
105106
| [![number badge]][number] | [![number version badge]][number npm page] |
@@ -119,6 +120,11 @@ Reusable pieces of code, which can be **reused** across the projects.
119120

120121
<!-- prettier-ignore-start -->
121122
<!-- PACKAGES LINKS -->
123+
[object]: ./packages/object/README.md
124+
[object badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-object&style=flat-square&color=informational
125+
[object version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-object/latest?style=flat-square&logo=npm
126+
[object npm page]: https://www.npmjs.com/package/@terminal-nerds/snippets-object
127+
122128
[url]: ./packages/url/README.md
123129
[url badge]: https://img.shields.io/static/v1?label=%40terminal-nerds&message=snippets-url&style=flat-square&color=informational
124130
[url version badge]: https://img.shields.io/npm/v/@terminal-nerds/snippets-url/latest?style=flat-square&logo=npm

packages/object/.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/object/CHANGELOG.md

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

packages/object/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/object/README.md

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

packages/object/package.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"$schema": "https://json.schemastore.org/package",
3+
"type": "module",
4+
"name": "@terminal-nerds/snippets-object",
5+
"version": "0.0.0",
6+
"description": "@terminal-nerds reusable snippets for objects.",
7+
"keywords": [
8+
"snippets",
9+
"typescript",
10+
"object"
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/object"
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+
},
40+
"files": [
41+
"dist/"
42+
],
43+
"scripts": {
44+
"build": "tsup",
45+
"clean": "concurrently \"pnpm:clean:*\" --group --timings",
46+
"clean:build": "del \"./dist\"",
47+
"clean:cache": "del \"./node_modules/.cache\"",
48+
"clean:test": "del \"./coverage\"",
49+
"dev:test": "vitest watch --ui",
50+
"dev:pkg": "tsup --watch",
51+
"fix": "concurrently \"pnpm:fix:*\" --group --timings",
52+
"fix:format": "pretty-quick --branch main --pattern \"packages/object/**/*\" --verbose",
53+
"fix:js": "pnpm lint:js --fix",
54+
"fix:md": "pnpm lint:md --fix",
55+
"fix:pkg": "syncpack format ; syncpack fix-mismatches",
56+
"lint": "concurrently \"pnpm:lint:*\" --group --timings",
57+
"lint:deps": "depcheck",
58+
"lint:format": "pnpm fix:format --check",
59+
"lint:js": "DEBUG=\"eslint:cli-engine\" eslint . --ext .cjs,.js,.ts,.json,.yml --cache --cache-location \"./node_modules/.cache/eslint\"",
60+
"lint:md": "markdownlint . --config \"../../.markdownlint.json\" --dot --ignore \"./LICENSE.md\" --ignore-path \"../../.gitignore\"",
61+
"lint:pkg": "syncpack list-mismatches",
62+
"lint:types": "tsc --noEmit",
63+
"test:unit": "vitest run --dir \"./source\""
64+
},
65+
"peerDependencies": {
66+
"typescript": "5.0.2"
67+
},
68+
"devDependencies": {
69+
"@terminal-nerds/snippets-test": "workspace:*"
70+
},
71+
"peerDependenciesMeta": {
72+
"typescript": {
73+
"optional": true
74+
}
75+
},
76+
"dependencies": {
77+
"@terminal-nerds/snippets-type": "workspace:*",
78+
"type-fest": "3.6.1",
79+
"zod": "3.21.4"
80+
}
81+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
2+
import { describe, it } from "vitest";
3+
4+
import { renameObjectKeys } from "./keys.ts";
5+
6+
const SAMPLE_OBJECT = {
7+
1: "one",
8+
two: 2,
9+
3: "threethree",
10+
four: "444444",
11+
5: 5,
12+
} as const;
13+
14+
describe("renameObjectKeys(object, renamer)", () => {
15+
const expected = {
16+
"11": "one",
17+
"two1": 2,
18+
"31": "threethree",
19+
"four1": "444444",
20+
"51": 5,
21+
} as const;
22+
23+
it(
24+
returns(expected).on(`passed sample object`).sample(SAMPLE_OBJECT).and(`with renamer appending 1 to each key`),
25+
({ expect }) => {
26+
expect(renameObjectKeys(SAMPLE_OBJECT, (key) => `${key}1` as const)).toEqual(expected);
27+
},
28+
);
29+
});

packages/object/source/keys/keys.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { z } from "zod";
2+
3+
const OBJECT_KEY_SCHEMA = z.number().or(z.string()).or(z.symbol());
4+
const RENAMER_SCHEMA = z.function().args(OBJECT_KEY_SCHEMA).returns(OBJECT_KEY_SCHEMA);
5+
type ObjectKey = number | string | symbol;
6+
7+
export function renameObjectKeys<OldKey extends ObjectKey, Value, NewKey extends ObjectKey>(
8+
object: Record<OldKey, Value>,
9+
renamer: (key: OldKey) => NewKey,
10+
): Record<NewKey, Value> {
11+
RENAMER_SCHEMA.parse(renamer);
12+
13+
return Object.fromEntries(
14+
(Object.entries(object) as Array<[OldKey, Value]>).map(([key, value]) => [renamer(key), value]),
15+
) as Record<NewKey, Value>;
16+
}

packages/object/source/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* MODULES */
2+
export * from "./keys/keys.ts";
3+
export * from "./schema/schema.ts";

0 commit comments

Comments
 (0)