"use strict"; const assert = require("assert"); const capitalize = require("capitalize"); const pickBestOption = require("./pick-best-option"); const mapManufacturer = require("./map-manufacturer"); const normalizeString = require("./normalize-string"); const syncpipe = require("syncpipe"); function fixCasing(name) { let normalized = normalizeString(name); // NOTE: We ignore <= 4 character manufacturer names, as those are likely to be abbreviations, and therefore should remain all-uppercase if (normalized.length > 4 && normalized === normalized.toUpperCase()) { // String is in all-caps return capitalize.words(normalized); } else { return normalized; } } module.exports = function createDatasheet(api, data) { let { createItem, mergeItem } = api; let url = normalizeString(data.url); if (url != null) { let productID = normalizeString(data.productID); let model = normalizeString(data.name); let description = normalizeString(data.description); let source = data.source; let priority = data.priority; let manufacturer = normalizeString(data.manufacturer); assert(manufacturer != null); assert(model != null); let mappedManufacturer = syncpipe(manufacturer, [ (_) => (data.fixCasing === true) ? fixCasing(_) : _, (_) => mapManufacturer(_) ]); let mappedID = `datasheet:${mappedManufacturer}:${model}`; let unmappedID = `datasheet:${manufacturer}:${model}`; createItem({ id: mappedID, update: (data) => pickBestOption(data, { priority: priority, source: source, manufacturer: mappedManufacturer, productID: productID, name: model, description: description, url: url }) }); if (mappedID !== unmappedID) { // NOTE: This is to get rid of items which were created before a mapping for that manufacturer existed; it essentially removes and redirects the old name into the new, normalized name. mergeItem({ from: unmappedID, into: mappedID, merge: (into, from) => pickBestOption(from, into) }); } } };