jurisdictions
Advanced tools
Comparing version
{ | ||
"name": "jurisdictions", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "access the public jurisdictions database from the Asia Pacific Foundation of Canada", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -7,2 +7,3 @@ import { graph as API } from './API.js' | ||
#phonebook = new Map() | ||
#callbacks = new Map() | ||
constructor(data){ | ||
@@ -22,2 +23,13 @@ this.lookup = this.lookup.bind(this) | ||
} | ||
addCallback(key,func){ | ||
this.#callbacks.set(key,func) | ||
return this | ||
} | ||
async readyWith(...callBackKeys){ | ||
await this.ready | ||
const results = callBackKeys.map( key => { | ||
if(this.#callbacks.has(key)) return this.#callbacks.get(key)(this) | ||
} ) | ||
return Promise.all(results).then(whatever=>this) | ||
} | ||
// id can/should be either a wikidataID (string) or a geo_id (number) | ||
@@ -24,0 +36,0 @@ // or an array of such |
import { JurisdictionGraph, Jurisdiction } from '../src/' | ||
import staticData from './staticGraphData.json' | ||
import { Twinning } from './connections/twinning.js' | ||
import twinsData from './twinning-data.json' | ||
function addTwins(graph){ | ||
twinsData.map( pair => { | ||
let [A,B] = graph.lookupNow([pair.a,pair.b]) | ||
new Twinning(A,B).notify() | ||
} ) | ||
} | ||
import { Mission } from './connections/mission.js' | ||
import dipMissions from './missions.json' | ||
function addMissions(graph){ | ||
dipMissions.map( missionData => { | ||
let operator = graph.lookupNow(missionData.operatorID) | ||
let destination = graph.lookupNow(missionData.destID) | ||
if(! operator || ! destination ){ | ||
return console.warn( `Mission ${missionData.missionID} missing at least one of these jurisdictions`, | ||
missionData.operatorID, missionData.destID ) | ||
} | ||
new Mission({operator,destination,missionData}).notify() | ||
} ) | ||
} | ||
import { TradeAgreement } from './connections/trade-agreement.js' | ||
import tradeAgreements from './canada-trade-agreements.json' | ||
function addAgreements(graph){ | ||
tradeAgreements.map( agreement => { | ||
let { signatories, ...data } = agreement | ||
let jurs = signatories.split(',').map(graph.lookupNow).filter(j=>j) | ||
new TradeAgreement(data,...jurs).notify() | ||
} ) | ||
} | ||
test('Build full graph without errors',() => { | ||
@@ -51,7 +73,5 @@ const graph = new JurisdictionGraph(staticData); | ||
const graph = new JurisdictionGraph(staticData); | ||
twinsData.map( pair => { | ||
let [A,B] = graph.lookupNow([pair.a,pair.b]) | ||
new Twinning(A,B).notify() | ||
} ) | ||
return graph.lookup('Q1037393').then( hokkaido => { | ||
graph.addCallback('twins',addTwins) | ||
return graph.readyWith('twins').then( graph => { | ||
let hokkaido = graph.lookupNow('Q1037393') | ||
expect(hokkaido.connections(/Twinning/,{descendants:true}).length).toBe(27) | ||
@@ -73,15 +93,5 @@ } ) | ||
expect(quebec.connections(/Mission/).length).toBe(0) | ||
dipMissions.map( missionData => { | ||
let operator = graph.lookupNow(missionData.operatorID) | ||
let destination = graph.lookupNow(missionData.destID) | ||
if(! operator || ! destination ){ | ||
return console.warn( `Mission ${missionData.missionID} missing at least one of these jurisdictions`, | ||
missionData.operatorID, missionData.destID ) | ||
} | ||
new Mission({operator,destination,missionData}).notify() | ||
} ) | ||
// this bit is async to run after the missions are added | ||
return graph.lookup('18').then( quebec => { | ||
graph.addCallback('missions',addMissions) | ||
return graph.readyWith('missions').then( graph => { | ||
const quebec = graph.lookupNow('18') | ||
expect(quebec.hasConnections(/Mission/)).toBe(true) | ||
@@ -99,8 +109,5 @@ let missionsSent = quebec.connections(/Mission/) | ||
const graph = new JurisdictionGraph(staticData); | ||
tradeAgreements.map( agreement => { | ||
let { signatories, ...data } = agreement | ||
let jurs = signatories.split(',').map(graph.lookupNow).filter(j=>j) | ||
new TradeAgreement(data,...jurs).notify() | ||
} ) | ||
return graph.lookup(30).then( HK => { | ||
graph.addCallback('trade',addAgreements) | ||
return graph.readyWith('trade').then(graph => { | ||
let HK = graph.lookupNow(30) | ||
expect(HK.connections(/TradeAgreement/,{ancestors:true}).length).toBe(2) | ||
@@ -107,0 +114,0 @@ expect(HK.connections(/TradeAgreement/).length).toBe(1) |
178460
0.34%781
3.03%