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 authored 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 = [
'[email protected]',
'foo@bar',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@livekit.io',
'!#$%&`*+/=?^`{|}[email protected]',
'[email protected]',
'a@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghikl.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.hij',
'[email protected]',
'"\\a"@livekit.io',
'""@livekit.io',
'"test"@livekit.io',
'"\\""@livekit.io',
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklmn@livekit.io',
'[email protected]',
'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',
'[email protected]',
'[email protected]',
'foo@[IPv6:2001:db8::2]',
];

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

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:[email protected]')?.[0]).toBe('[email protected]');
});

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.