Skip to content

Commit

Permalink
Create emailregex in components-core instead of using a dedicated pac…
Browse files Browse the repository at this point in the history
…kage (#895)
  • Loading branch information
lukasIO committed Jun 14, 2024
1 parent 857f77e commit f094912
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-buses-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/components-core": patch
---

Create emailregex in components-core instead of using a dedicated package
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"typings": "dist/index.d.ts",
"dependencies": {
"@floating-ui/dom": "1.6.5",
"email-regex": "5.0.0",
"loglevel": "1.9.1",
"rxjs": "7.8.1"
},
Expand Down
65 changes: 65 additions & 0 deletions packages/core/src/helper/emailRegex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, test } from 'vitest';
import { createEmailRegExp } from './emailRegex';

const fixtures = [
'livekit@gmail.com',
'foo@bar',
'test@about.museum',
'test@nominet.org.uk',
'test.test@livekit.io',
'test@255.255.255.255',
'a@livekit.io',
'test@e.com',
'test@xn--hxajbheg2az3al.xn--jxalpdlp',
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@livekit.io',
'!#$%&`*+/=?^`{|}~@livekit.io',
'test@g--a.com',
'a@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.hij',
'123@livekit.io',
'"\\a"@livekit.io',
'""@livekit.io',
'"test"@livekit.io',
'"\\""@livekit.io',
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklmn@livekit.io',
'test@iana.co-uk',
'a@a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v',
'test@foo-bar.com',
'foo@x.solutions',
'foo@[IPv6:2001:db8::2]',
];

const fixturesNot = [
'@',
'@io',
'@livekit.io',
'test..livekit.io',
'test@iana..com',
'test@livekit.io.',
'.test@livekit.io',
'livekit@livekit@livekit.com',
'mailto:livekit@gmail.com',
'foo.example.com',
'test.@example.com',
];

describe('Email regex tests', () => {
test('extract', (t) => {
for (const fixture of fixtures) {
t.expect((createEmailRegExp().exec(`foo ${fixture} bar`) || [])[0]).toBe(fixture);
}

t.expect(createEmailRegExp().exec('mailto:livekit@gmail.com')?.[0]).toBe('livekit@gmail.com');
});

test('exact', (t) => {
for (const fixture of fixtures) {
t.expect(createEmailRegExp({ exact: true }).test(fixture)).toBeTruthy();
}
});

test('failures', (t) => {
for (const fixture of fixturesNot) {
t.expect(createEmailRegExp({ exact: true }).test(fixture)).toBeFalsy();
}
});
});
7 changes: 6 additions & 1 deletion packages/core/src/helper/emailRegex.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import createEmailRegExp from 'email-regex';
// source code adapted from https://github.com/sindresorhus/email-regex due to ESM import incompatibilities when trying to serve a CJS version of components

const regex = '[^\\.\\s@:](?:[^\\s@:]*[^\\s@:\\.])?@[^\\.\\s@]+(?:\\.[^\\.\\s@]+)*';

function createEmailRegExp({ exact }: { exact?: boolean } = {}) {
return exact ? new RegExp(`^${regex}$`) : new RegExp(regex, 'g');
}
export { createEmailRegExp };
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f094912

Please sign in to comment.