phasmid 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +267 -0
  2. package/package.json +52 -0
package/README.md ADDED
@@ -0,0 +1,267 @@
1
+ # Phasmid
2
+
3
+ ![Phasmid Logo](https://raw.githubusercontent.com/Smiduweorc/phasmid/refs/heads/master/assets/logo.png)
4
+
5
+ Phasmid is provider-aware email normalization and canonicalization for the browser and the server. Pure ESM, zero runtime dependencies, fully typed.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install phasmid
11
+ ```
12
+
13
+ Phasmid ships as ESM (`"type": "module"`). It runs in modern browsers and in Node 18+, and has no runtime dependencies.
14
+
15
+ ## Quick start
16
+
17
+ ```ts
18
+ import { normalizeEmail, isSameEmail, getEmailProvider } from "phasmid";
19
+
20
+ normalizeEmail("John.Doe+newsletter@googlemail.com"); // "johndoe@gmail.com"
21
+ normalizeEmail("John.Doe+news@outlook.com"); // "john.doe@outlook.com" (dots kept)
22
+ normalizeEmail("john-shopping@yahoo.com"); // "john@yahoo.com" (Yahoo uses '-')
23
+ normalizeEmail("Jane@Example.COM"); // "Jane@example.com" (unknown domain: conservative)
24
+
25
+ isSameEmail("a.b@gmail.com", "ab+promo@gmail.com"); // true
26
+ getEmailProvider("x@hotmail.co.uk"); // "microsoft"
27
+ ```
28
+
29
+ ## Why canonicalization?
30
+
31
+ Mail providers apply their own rules to decide which mailbox an address reaches:
32
+
33
+ | Behavior | Example | Same mailbox? |
34
+ | ------------------------------ | ---------------------------------------------------- | ------------- |
35
+ | **Plus/sub-address tagging** | `you+anything@gmail.com` -> `you@gmail.com` | yes |
36
+ | **Dot-insensitivity** (Gmail) | `y.o.u@gmail.com` -> `you@gmail.com` | yes |
37
+ | **Alias domains** (Gmail) | `you@googlemail.com` -> `you@gmail.com` | yes |
38
+ | **Case-insensitivity** | `You@gmail.com` -> `you@gmail.com` | yes |
39
+
40
+ Phasmid applies the right rules for each provider and returns one canonical string, so equal mailboxes compare equal.
41
+
42
+ ## API reference
43
+
44
+ Every function takes an optional `options` object (see [Configuration](#configuration)).
45
+
46
+ ### `normalizeEmail(email, options?) => string`
47
+
48
+ Returns the canonical form of `email`. Throws `TypeError` if `email` is not a string. Malformed input (no `@`, empty local/domain) is returned trimmed and unchanged.
49
+
50
+ ```ts
51
+ normalizeEmail(" Foo.Bar+spam@GMAIL.com "); // "foobar@gmail.com"
52
+ ```
53
+
54
+ ### `normalizeEmailDetailed(email, options?) => NormalizedEmail`
55
+
56
+ Like `normalizeEmail`, but returns the full breakdown:
57
+
58
+ ```ts
59
+ normalizeEmailDetailed("John.Doe+promo@gmail.com");
60
+ // {
61
+ // normalized: "johndoe@gmail.com",
62
+ // local: "johndoe",
63
+ // domain: "gmail.com",
64
+ // providerId: "gmail", // null for unknown domains
65
+ // subaddress: "promo", // the stripped tag, or null
66
+ // valid: true, // does it look like a syntactically valid address?
67
+ // }
68
+ ```
69
+
70
+ ### `isSameEmail(a, b, options?) => boolean`
71
+
72
+ `true` when `a` and `b` normalize to the same canonical address (i.e. deliver to the same mailbox under the configured rules).
73
+
74
+ ```ts
75
+ isSameEmail("J.Doe+work@gmail.com", "jdoe@googlemail.com"); // true
76
+ isSameEmail("a@outlook.com", "a@hotmail.com"); // false (distinct mailboxes)
77
+ ```
78
+
79
+ ### `getEmailProvider(email, options?) => string | null`
80
+
81
+ Returns the id of the provider that owns the address's domain, or `null` if no provider matches (or the input is not a valid address). Never throws.
82
+
83
+ ```ts
84
+ getEmailProvider("a@proton.me"); // "proton"
85
+ getEmailProvider("a@example.com"); // null
86
+ ```
87
+
88
+ ### `DEFAULT_PROVIDERS`
89
+
90
+ The read-only array of built-in [`ProviderRule`](#options-reference) objects, exported so you can inspect or build on top of it.
91
+
92
+ ```ts
93
+ import { DEFAULT_PROVIDERS } from "phasmid";
94
+ DEFAULT_PROVIDERS.flatMap((p) => p.domains); // every recognized domain
95
+ ```
96
+
97
+ ## Built-in providers
98
+
99
+ | id | Separator | Removes dots | Alias domain | Notable domains |
100
+ | ----------- | :-------: | :----------: | ------------ | ----------------------------------------- |
101
+ | `gmail` | `+` | yes | `gmail.com` | gmail.com, googlemail.com |
102
+ | `microsoft` | `+` | no | none | outlook.\*, hotmail.\*, live.\*, msn.com |
103
+ | `yahoo` | `-` | no | none | yahoo.\*, ymail.com, rocketmail.com |
104
+ | `icloud` | `+` | no | none | icloud.com, me.com, mac.com |
105
+ | `fastmail` | `+` | no | none | fastmail.com, fastmail.fm |
106
+ | `proton` | `+` | no | none | protonmail.com, proton.me, pm.me |
107
+ | `yandex` | `+` | no | none | yandex.\*, ya.ru |
108
+ | `zoho` | `+` | no | none | zoho.com, zohomail.com, zoho.eu |
109
+ | `mailfence` | `+` | no | none | mailfence.com |
110
+ | `runbox` | `+` | no | none | runbox.com |
111
+ | `pobox` | `+` | no | none | pobox.com |
112
+ | `tutanota` | `+` | no | none | tuta.com, tutanota.com, keemail.me |
113
+ | `posteo` | `+` | no | none | posteo.de, posteo.net |
114
+ | `mailbox` | `+` | no | none | mailbox.org |
115
+ | `aol` | none | no | none | aol.com, aim.com |
116
+
117
+ All built-in providers lowercase the local part (they are case-insensitive in practice).
118
+
119
+ > **Unknown domains** get a **conservative** treatment: the domain is lowercased and the
120
+ > local part is left **untouched**. The email spec (RFC 5321) permits case-sensitive local
121
+ > parts, and distinct mailboxes must not be merged by accident. Opt into more aggressive
122
+ > behavior with [`defaultRule`](#a-default-rule-for-every-domain).
123
+
124
+ ## Configuration
125
+
126
+ ### Add your own provider
127
+
128
+ Pass extra rules via `providers`. They are matched by domain and take precedence over the built-ins.
129
+
130
+ ```ts
131
+ import { normalizeEmail, type ProviderRule } from "phasmid";
132
+
133
+ const corporate: ProviderRule = {
134
+ id: "corp",
135
+ domains: ["mycompany.com", "mycompany.co"],
136
+ canonicalDomain: "mycompany.com", // collapse the alias
137
+ lowercaseLocal: true,
138
+ removeDots: true,
139
+ subaddressSeparators: ["+"],
140
+ };
141
+
142
+ normalizeEmail("John.Doe+x@mycompany.co", { providers: [corporate] });
143
+ // "johndoe@mycompany.com"
144
+ ```
145
+
146
+ ### Override a built-in provider
147
+
148
+ A user provider that lists an existing domain wins, letting you change behavior per domain:
149
+
150
+ ```ts
151
+ // Treat gmail.com strictly: keep dots, don't collapse googlemail, just lowercase.
152
+ normalizeEmail("John.Doe@gmail.com", {
153
+ providers: [{ id: "gmail-strict", domains: ["gmail.com"], lowercaseLocal: true }],
154
+ });
155
+ // "john.doe@gmail.com"
156
+ ```
157
+
158
+ ### Replace all providers
159
+
160
+ Ignore the built-ins entirely and use only your own:
161
+
162
+ ```ts
163
+ normalizeEmail("a@gmail.com", {
164
+ replaceDefaultProviders: true,
165
+ providers: [{ id: "only", domains: ["only.com"], subaddressSeparators: ["+"] }],
166
+ });
167
+ // gmail.com now matches nothing -> conservative default
168
+ ```
169
+
170
+ ### A default rule for every domain
171
+
172
+ Apply rules to domains that match no provider, e.g. strip `+tags` everywhere:
173
+
174
+ ```ts
175
+ normalizeEmail("john+tag@example.com", {
176
+ defaultRule: { lowercaseLocal: true, subaddressSeparators: ["+"] },
177
+ });
178
+ // "john@example.com"
179
+ ```
180
+
181
+ A `defaultRule` never overrides a matched provider (Yahoo still uses `-`, etc.).
182
+
183
+ ### Options reference
184
+
185
+ ```ts
186
+ interface NormalizeOptions {
187
+ providers?: ProviderRule[]; // extra/override rules (win by domain)
188
+ replaceDefaultProviders?: boolean; // ignore the built-ins entirely (default: false)
189
+ defaultRule?: DefaultRule; // rule for unmatched domains
190
+ lowercaseDomain?: boolean; // default: true
191
+ }
192
+
193
+ interface ProviderRule {
194
+ id: string; // stable identifier, e.g. "gmail"
195
+ domains: string[]; // domains this rule applies to (case-insensitive)
196
+ canonicalDomain?: string; // collapse all matched domains to this one
197
+ lowercaseLocal?: boolean; // lowercase the local part
198
+ removeDots?: boolean; // strip dots from the local part (Gmail)
199
+ subaddressSeparators?: string[]; // tag separators, e.g. ["+"] or ["-"]
200
+ }
201
+
202
+ // DefaultRule is a ProviderRule without `id`, `domains`, or `canonicalDomain`.
203
+ ```
204
+
205
+ ## Recipes
206
+
207
+ **Deduplicate a list of addresses**
208
+
209
+ ```ts
210
+ import { normalizeEmail } from "phasmid";
211
+
212
+ const unique = [...new Map(
213
+ rawEmails.map((e) => [normalizeEmail(e), e]),
214
+ ).values()];
215
+ ```
216
+
217
+ **Block re-registration with an aliased address**
218
+
219
+ ```ts
220
+ import { isSameEmail } from "phasmid";
221
+
222
+ const alreadyUsed = existingUsers.some((u) => isSameEmail(u.email, signup.email));
223
+ ```
224
+
225
+ **Store a canonical key alongside the original**
226
+
227
+ ```ts
228
+ const { normalized, valid } = normalizeEmailDetailed(input);
229
+ if (!valid) throw new Error("Invalid email");
230
+ await db.users.insert({ email: input, emailKey: normalized });
231
+ ```
232
+
233
+ ## Edge cases & guarantees
234
+
235
+ - The address is split on the **last** `@`.
236
+ - **Quoted** local parts (`"a..b"@x.com`) are preserved verbatim, with no dot/tag transforms.
237
+ - A separator at index 0 (`+tag@gmail.com`) is **ignored**; stripping it would empty the local part.
238
+ - Only the **first** separator is used as the cut point (`a+b+c` becomes `a`).
239
+ - Domains are lowercased by default; the matched provider's `canonicalDomain` (if any) wins.
240
+ - Non-string input throws `TypeError`. Malformed input is returned unchanged with `valid: false`.
241
+
242
+ ## Limitations
243
+
244
+ - `valid` is a lightweight syntactic check, **not** full RFC 5322 validation or MX verification.
245
+ - Provider rules reflect widely-documented behavior at the time of writing; providers can change. Everything is overridable via options.
246
+ - Fastmail-style **subdomain addressing** (`tag@user.fastmail.com`) is not resolved, because it depends on the account's domain layout.
247
+
248
+ ## Development
249
+
250
+ ```bash
251
+ npm install # install dependencies
252
+ npm run build # compile TypeScript to dist/ (tsc)
253
+ npm run typecheck # type-check the sources and the tests
254
+ npm run lint # eslint
255
+ npm test # run the test suite (node:test via tsx)
256
+ npm run docs # generate API docs to docs/ (typedoc)
257
+ ```
258
+
259
+ Tests live in `tests/` as `*.test.ts` files. They run directly against the TypeScript sources with `node --import tsx --test`, so no build step is needed to run them. CI (`.github/workflows/ci.yml`) lints, type-checks, tests, and builds on every push and pull request across Linux, macOS, and Windows on Node 22 and 24.
260
+
261
+ ## License
262
+
263
+ ISC
264
+
265
+ ## Commits
266
+
267
+ The commits in this repo are a bit more deliberate, as this small library is not on GitLab.
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "phasmid",
3
+ "version": "0.0.1",
4
+ "description": "TypeScript library for data transformation and normalization.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/types/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tests/tsconfig.json",
20
+ "lint": "eslint .",
21
+ "lint:fix": "eslint . --fix",
22
+ "test": "node --import tsx --test \"tests/**/*.test.ts\"",
23
+ "docs": "typedoc",
24
+ "changelog": "git-cliff -o CHANGELOG.md",
25
+ "prepare": "lefthook install"
26
+ },
27
+ "keywords": [
28
+ "typescript",
29
+ "esm",
30
+ "normalization",
31
+ "transformation"
32
+ ],
33
+ "author": "",
34
+ "license": "ISC",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/Smiduweorc/phasmid.git"
38
+ },
39
+ "devDependencies": {
40
+ "@commitlint/cli": "^21.2.2",
41
+ "@commitlint/config-conventional": "^21.2.2",
42
+ "@eslint/js": "^10.0.1",
43
+ "@types/node": "^26.5.0",
44
+ "eslint": "^10.10.0",
45
+ "git-cliff": "^2.13.1",
46
+ "lefthook": "^2.1.12",
47
+ "tsx": "^4.23.13",
48
+ "typedoc": "^0.28.20",
49
+ "typescript": "^6.0.3",
50
+ "typescript-eslint": "^8.70.0"
51
+ }
52
+ }