csi-master-format-cost-codes
const got = require('got')
const { default: ow } = require('ow')
const registeredIdHelper = {
'defaults': {
'description': null,
'id': null,
'registrationAuthority':'Construction Specifications Institute (CSI)',
'type':'master-format-cost-code',
'validFrom':'2012-04-01',
'validTo':null
},
'required': [
'description',
'id'
],
validate (constructorArgs) {
const { required } = registeredIdHelper
ow(constructorArgs, ow.object.hasKeys(...required))
}
}
class RegisteredIdentifier {
constructor(properties = {}) {
const { defaults, validate } = registeredIdHelper
validate(properties)
Object.assign(this, defaults, properties)
}
valueOf() {
return this.id
}
}
const DFN_BASE_URL = 'https://www.arcat.com/divs/sec/sec'
const DFN_FILE_EXT = '.shtml'
const NO_WHITESPACE = ''
class CostCode extends RegisteredIdentifier {
toString() {
const section = this.valueOf().replace(/\s+/g, NO_WHITESPACE)
return `${DFN_BASE_URL}${section}${DFN_FILE_EXT}`
}
}
const CODES_URL = 'https://gitlab.com/better-family-demolition/construction-codes/-/snippets/2188796/raw/main/csi-master-format-cost-codes-2012-04-01.json'
const initCostCodes = (ref, costCodeList) => {
const value = costCodeList.map(attrs => new CostCode(attrs))
Object.defineProperty(ref, 'costCodes', {
enumerable: true,
value,
writable: false
})
}
/** Creates data objects with properties
that represent CSI MasterFormat® standards. */
class MasterFormat {
/** Create a data object. */
constructor(costCodes=[]) {
initCostCodes(this, costCodes)
}
/**
* Create a MasterFormat.prototype with
* all 7,533 construction cost codes.
* @param {String} codesUrl - A URL of Party.RegisteredIdentifiers, formatted as JSON.
* @return {MasterFormat} A MasterFormat.prototype with cost codes.
*/
static async factory (codesUrl=CODES_URL) {
let masterFormat = null
try {
const codes = await got.get(codesUrl).json()
masterFormat = new MasterFormat(codes)
} catch (err) {
console.error(err)
throw err
}
return masterFormat
}
}
const main = async () => {
const CODE_ID = '02 31 13'
const masterFormat = await MasterFormat.factory()
const costCode = masterFormat
.costCodes
.filter(({ id }) => id === CODE_ID)
.shift()
console.info('MasterFormat.factory creates a MasterFormat.prototype object with %d cost codes', masterFormat.costCodes.length)
console.log('valueOf() returns the MasterFormat.prototype#id value, e.g., "%s"', costCode.valueOf())
console.log('toString() generates a definition URL ⬇︎', costCode.toString())
console.log('JSON.stringify serializes the object ⬇︎', JSON.stringify(costCode, null, ' '))
console.table(costCode)
}
no comments