Skip to content

Commit

Permalink
Fix to use import
Browse files Browse the repository at this point in the history
  • Loading branch information
HashidaTKS committed Sep 24, 2024
1 parent 464d9d3 commit 16d88b6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/web/dialog.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" type="application/javascript" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script type="module" src="recipient-parser.mjs"></script>
<script type="module" src="recipient-classifier.mjs"></script>
<script src="dialog.js" type="application/javascript"></script>
<script src="dialog.css" type="stylesheet"></script>
<link rel="stylesheet" href="dialog.css">
Expand Down
66 changes: 1 addition & 65 deletions src/web/dialog.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,4 @@
function parse(recipient) {
const address = /<([^@]+@[^>]+)>\s*$/.test(recipient) ? RegExp.$1 : recipient;
const domain = address.split("@")[1].toLowerCase();
return {
recipient,
address,
domain,
};
}

class RecipientClassifier {
constructor({ internalDomains } = {}) {
const uniquePatterns = new Set(
(internalDomains || [])
.filter((pattern) => !pattern.startsWith("#")) // reject commented out items
.map(
(pattern) =>
pattern
.toLowerCase()
.replace(/^(-?)@/, "$1") // delete needless "@" from domain only patterns: "@example.com" => "example.com"
.replace(/^(-?)(?![^@]+@)/, "$1*@") // normalize to full address patterns: "[email protected]" => "[email protected]", "example.com" => "*@example.com"
)
);
const negativeItems = new Set(
[...uniquePatterns].filter((pattern) => pattern.startsWith("-")).map((pattern) => pattern.replace(/^-/, ""))
);
for (const negativeItem of negativeItems) {
uniquePatterns.delete(negativeItem);
uniquePatterns.delete(`-${negativeItem}`);
}
this.$internalPatternsMatcher = new RegExp(
`^(${[...uniquePatterns].map((pattern) => this.$toRegExpSource(pattern)).join("|")})$`,
"i"
);
this.classify = this.classify.bind(this);
}

$toRegExpSource(source) {
// https://stackoverflow.com/questions/6300183/sanitize-string-of-regex-characters-before-regexp-build
const sanitized = source.replace(/[#-.]|[[-^]|[?|{}]/g, "\\$&");

const wildcardAccepted = sanitized.replace(/\\\*/g, ".*").replace(/\\\?/g, ".");

return wildcardAccepted;
}

classify(recipients) {
const internals = new Set();
const externals = new Set();

for (const recipient of recipients) {
const classifiedRecipient = {
...parse(recipient),
};
const address = classifiedRecipient.address;
if (this.$internalPatternsMatcher.test(address)) internals.add(classifiedRecipient);
else externals.add(classifiedRecipient);
}

return {
internals: Array.from(internals),
externals: Array.from(externals),
};
}
}
import {RecipientClassifier} from "./recipient-classifier.mjs";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Office.initialize = function (reason) {};
Expand Down

0 comments on commit 16d88b6

Please sign in to comment.