From 62d3744a3fe021d04cccef188a774f55588a24cc Mon Sep 17 00:00:00 2001 From: Huey Date: Wed, 16 Jun 2021 12:26:59 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20new:=20custom=20list=20of=20cases;?= =?UTF-8?q?=20resolve=20#22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 18 ++++++++++++-- package.json | 3 ++- src/utils/Constants.ts | 6 +++++ src/utils/scraper/UK/UK.ts | 3 +++ src/utils/scraper/custom/Custom.ts | 37 ++++++++++++++++++++++++++++ src/utils/scraper/custom/CustomDB.ts | 36 +++++++++++++++++++++++++++ src/utils/scraper/custom/index.ts | 1 + 7 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/utils/scraper/custom/Custom.ts create mode 100644 src/utils/scraper/custom/CustomDB.ts create mode 100644 src/utils/scraper/custom/index.ts diff --git a/package-lock.json b/package-lock.json index e77d86f..317e49b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,18 @@ { "name": "clerkent", - "version": "2.5.0", + "version": "2.7.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "2.5.0", + "version": "2.7.1", "license": "GPL-3", "dependencies": { "@babel/runtime": "^7.14.0", "axios": "^0.21.1", "axios-cache-adapter": "^2.7.3", "cheerio": "^1.0.0-rc.9", + "fuse.js": "^6.4.6", "gestalt-pattern-matcher": "^0.0.12", "memoizee": "^0.4.15", "qs": "^6.10.1", @@ -9702,6 +9703,14 @@ "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "dev": true }, + "node_modules/fuse.js": { + "version": "6.4.6", + "resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-6.4.6.tgz", + "integrity": "sha512-/gYxR/0VpXmWSfZOIPS3rWwU8SHgsRTwWuXhyb2O6s7aRuVtHtxCkR33bNYu3wyLyNx/Wpv0vU7FZy8Vj53VNw==", + "engines": { + "node": ">=10" + } + }, "node_modules/gauge": { "version": "2.7.4", "resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz", @@ -30641,6 +30650,11 @@ "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "dev": true }, + "fuse.js": { + "version": "6.4.6", + "resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-6.4.6.tgz", + "integrity": "sha512-/gYxR/0VpXmWSfZOIPS3rWwU8SHgsRTwWuXhyb2O6s7aRuVtHtxCkR33bNYu3wyLyNx/Wpv0vU7FZy8Vj53VNw==" + }, "gauge": { "version": "2.7.4", "resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz", diff --git a/package.json b/package.json index 87d10cc..927d05b 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "axios": "^0.21.1", "axios-cache-adapter": "^2.7.3", "cheerio": "^1.0.0-rc.9", + "fuse.js": "^6.4.6", "gestalt-pattern-matcher": "^0.0.12", "memoizee": "^0.4.15", "qs": "^6.10.1", @@ -132,4 +133,4 @@ "wext-manifest-loader": "^2.3.0", "wext-manifest-webpack-plugin": "^1.2.1" } -} \ No newline at end of file +} diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index b8bcef4..dc0bd9d 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -172,6 +172,12 @@ const MISC_DATABASES = { name: `CommonLII`, url: `http://www.commonlii.org`, }, + custom: { + icon: ``, + id: `custom`, + name: `Clerkent`, + url: `#`, + }, } const INSTITUTIONAL_LOGINS = { diff --git a/src/utils/scraper/UK/UK.ts b/src/utils/scraper/UK/UK.ts index 4cd0b47..a215a9b 100644 --- a/src/utils/scraper/UK/UK.ts +++ b/src/utils/scraper/UK/UK.ts @@ -2,6 +2,7 @@ import type Law from '../../../types/Law' import BAILII from './BAILII' import Common from '../common' import LegislationGovUk from './LegislationGovUk' +import Custom from '../custom' import Logger from '../../Logger' import Helpers from '../../Helpers' import { sortUKCitations } from '../../Finder/CaseCitationFinder/UK' @@ -12,6 +13,7 @@ const getLegislation = LegislationGovUk.getLegislation const getCaseByName = async (caseName: string): Promise => { try { const results = (await Promise.allSettled([ + Custom.getCaseByName(caseName), BAILII.getCaseByName(caseName), Common.CommonLII.getCaseByName(caseName, Constants.JURISDICTIONS.UK.name), ])) @@ -35,6 +37,7 @@ const getCaseByName = async (caseName: string): Promise => { const getCaseByCitation = async (citation: string, court: string): Promise => { try { const results = (await Promise.allSettled([ + Custom.getCaseByCitation(citation, court), BAILII.getCaseByCitation(citation), Common.CommonLII.getCaseByCitation(citation), ])).filter(({ status }) => status === `fulfilled`) diff --git a/src/utils/scraper/custom/Custom.ts b/src/utils/scraper/custom/Custom.ts new file mode 100644 index 0000000..f68a2be --- /dev/null +++ b/src/utils/scraper/custom/Custom.ts @@ -0,0 +1,37 @@ +import Fuse from 'fuse.js' +import type Law from "../../../types/Law" +import CustomDB from './CustomDB' +import type { RawCase } from './CustomDB' + +const { cases: CustomCases } = CustomDB + +const toLawCase = ({ citations, ...others }: RawCase): Law.Case => ({ + ...others, + citation: citations[0], + }) + +const getCaseByName = async (caseName: string): Promise => { + const fuse = new Fuse(CustomCases.map(({ name }) => name), {}) + return fuse + .search(caseName) + .map(({ refIndex }) => CustomCases[refIndex]) + .map(rawCase => toLawCase(rawCase)) +} + +const getCaseByCitation = async (citation: string, court: string): Promise => Promise.resolve( + CustomCases + .filter(({ citations }) => + citations.some((cit) => (new RegExp(`${citation}`, `i`)).test(cit)), + ) + .map(({ citations, ...others }) => ({ + ...others, + citation: citations[0], + })), +) + +const Custom = { + getCaseByCitation, + getCaseByName, +} + +export default Custom diff --git a/src/utils/scraper/custom/CustomDB.ts b/src/utils/scraper/custom/CustomDB.ts new file mode 100644 index 0000000..65af5ac --- /dev/null +++ b/src/utils/scraper/custom/CustomDB.ts @@ -0,0 +1,36 @@ +import type Law from "../../../types/Law" +import Constants from "../../Constants" + +export interface RawCase extends Omit { + citations: Law.Case[`citation`][], +} + +const CustomDBCases: RawCase[] = [ + { + citations: [ + `[1957] 1 WLR 582`, + `[1957] 2 All ER 118​`, + ], + database: Constants.DATABASES.custom, + jurisdiction: Constants.JURISDICTIONS.UK.id, + links: [ + { + doctype: `Judgment`, + filetype: `HTML`, + url: `https://web.archive.org/web/20160122042428/http://oxcheps.new.ox.ac.uk:80/casebook/Resources/BOLAMV_1%20DOC.pdf`, + }, + { + doctype: `Judgment`, + filetype: `PDF`, + url: `https://web.archive.org/web/20160122042428if_/http://oxcheps.new.ox.ac.uk:80/casebook/Resources/BOLAMV_1%20DOC.pdf`, + }, + ], + name: `Bolam v Friern Hospital Management Committee`, + }, +] + +const CustomDB = { + cases: CustomDBCases, +} + +export default CustomDB \ No newline at end of file diff --git a/src/utils/scraper/custom/index.ts b/src/utils/scraper/custom/index.ts new file mode 100644 index 0000000..15794b7 --- /dev/null +++ b/src/utils/scraper/custom/index.ts @@ -0,0 +1 @@ +export { default } from './Custom' \ No newline at end of file