You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB
JavaScript

"use strict";
const assert = require("assert");
const pickBestOption = require("./pick-best-option");
const mapManufacturer = require("./map-manufacturer");
const normalizeString = require("./normalize-string");
module.exports = function createDatasheet(api, data) {
let { createItem, mergeItem } = api;
let productID = normalizeString(data.productID);
let manufacturer = normalizeString(data.manufacturer);
let model = normalizeString(data.name);
let description = normalizeString(data.description);
let url = normalizeString(data.url);
let source = data.source;
let priority = data.priority;
assert(manufacturer != null);
assert(model != null);
if (url != null) {
let mappedManufacturer = mapManufacturer(manufacturer);
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)
});
}
}
};