centralise msgs in copy/en.js

plus some other fixes
This commit is contained in:
Lachlan Kermode
2018-12-06 16:22:10 +00:00
parent 4da10b1409
commit 72edac944c
6 changed files with 72 additions and 56 deletions

View File

@@ -2,6 +2,12 @@
/**
* Model is a class whose sole responsibility is to save and load blueprints.
* It allows for different storage mechanisms for different kinds of blueprints.
*
* ERRORS:
* When a load function fails, it should throw either:
* 1. A __ error if the resource doesn't exist on that sheet/tab.
* 2. A __ error if a fragment lookup fails because it doesn't exist.
* 3. A __ error if something else goes wrong.
*/
class Model {
/**

View File

@@ -1,5 +1,6 @@
import fs from 'mz/fs'
import { fmtSourceTitle } from '../lib/util'
import copy from '../copy/en'
const STORAGE_DIRNAME = 'temp'
@@ -22,39 +23,36 @@ class StoreJson {
const fname = `${STORAGE_DIRNAME}/${parts[0]}__${parts[1]}__${
parts[2]
}.json`
return fs
.exists(fname)
.then(isAvailable => {
if (isAvailable) return fs.readFile(fname, 'utf8')
else {
throw new Error('No resource exists')
}
})
.then(data => JSON.parse(data))
.then(data => {
if (parts.length === 3) {
// No lookup if the requested url doesn't have a fragment
return data
} else if (parts[2] === 'ids') {
// Do a lookup if fragment is included to filter a relevant item
// When the resource requested is 'ids'
const id = parseInt(parts[3])
if (!isNaN(id) && id >= 0 && id < data.length) {
return data[id]
if (fs.existsSync(fname)) {
return fs.readFile(fname, 'utf8')
.then(data => JSON.parse(data))
.then(data => {
if (parts.length === 3) {
// No lookup if the requested url doesn't have a fragment
return data
} else if (parts[2] === 'ids') {
// Do a lookup if fragment is included to filter a relevant item
// When the resource requested is 'ids'
const id = parseInt(parts[3])
if (!isNaN(id) && id >= 0 && id < data.length) {
return data[id]
} else {
throw new Error(copy.errors.noFragment(parts))
}
} else {
throw new Error(`Fragment index does not exist`)
// Do a lookup if fragment is included to filter a relevant item
const index = parseInt(parts[3])
if (!isNaN(index) && index >= 0 && index < data.length) {
console.log(data, index)
return data.filter((vl, idx) => idx === index)[0]
} else {
throw new Error(copy.errors.noFragment(parts))
}
}
} else {
// Do a lookup if fragment is included to filter a relevant item
const index = parseInt(parts[3])
if (!isNaN(index) && index >= 0 && index < data.length) {
console.log(data, index)
return data.filter((vl, idx) => idx === index)[0]
} else {
throw new Error(`Fragment index does not exist`)
}
}
})
})
} else {
return Promise.reject(new Error(copy.errors.noResource(parts)))
}
}
// TODO: add method to build blueprint from data source