basant307/AI_Governance_Project
048
1const minDisplayHeight = 16;2const maxDisplayHeight = 24;3/**4* Check if displayHeight value is valid, returns 0 on failure5*/6function validateDisplayHeight(value) {7 while (value < minDisplayHeight) value *= 2;8 while (value > maxDisplayHeight) value /= 2;9 return value === Math.round(value) && value >= minDisplayHeight && value <= maxDisplayHeight ? value : 0;10}11/**12* Convert data to valid CollectionInfo13*/14function convertIconSetInfo(data, expectedPrefix = "") {15 if (typeof data !== "object" || data === null) return null;16 const source = data;17 const getSourceNestedString = (field, key, defaultValue = "") => {18 if (typeof source[field] !== "object") return defaultValue;19 const obj = source[field];20 return typeof obj[key] === "string" ? obj[key] : defaultValue;21 };22 let name;23 if (typeof source.name === "string") name = source.name;24 else if (typeof source.title === "string") name = source.title;25 else return null;26 if (expectedPrefix !== "" && typeof source.prefix === "string" && source.prefix !== expectedPrefix) return null;27 const info = { name };28 switch (typeof source.total) {29 case "number":30 info.total = source.total;31 break;32 case "string": {33 const num = parseInt(source.total);34 if (num > 0) info.total = num;35 break;36 }37 }38 if (typeof source.version === "string") info.version = source.version;39 info.author = { name: getSourceNestedString("author", "name", typeof source.author === "string" ? source.author : "") };40 if (typeof source.author === "object") {41 const sourceAuthor = source.author;42 if (typeof sourceAuthor.url === "string") info.author.url = sourceAuthor.url;43 }44 info.license = { title: getSourceNestedString("license", "title", typeof source.license === "string" ? source.license : "") };45 if (typeof source.license === "object") {46 const sourceLicense = source.license;47 if (typeof sourceLicense.spdx === "string") info.license.spdx = sourceLicense.spdx;48 if (typeof sourceLicense.url === "string") info.license.url = sourceLicense.url;49 }50 if (source.samples instanceof Array) {51 const samples = [];52 source.samples.forEach((item) => {53 if (typeof item === "string" && !samples.includes(item)) samples.push(item);54 });55 if (samples.length) info.samples = samples;56 }57 if (typeof source.height === "number" || typeof source.height === "string") {58 const num = parseInt(source.height);59 if (num > 0) info.height = num;60 }61 if (source.height instanceof Array) {62 source.height.forEach((item) => {63 const num = parseInt(item);64 if (num > 0) {65 if (!(info.height instanceof Array)) info.height = [];66 info.height.push(num);67 }68 });69 switch (info.height.length) {70 case 0:71 delete info.height;72 break;73 case 1: info.height = info.height[0];74 }75 }76 if (typeof info.height === "number") {77 const displayHeight = validateDisplayHeight(info.height);78 if (displayHeight && displayHeight !== info.height) info.displayHeight = displayHeight;79 }80 ["samplesHeight", "displayHeight"].forEach((prop) => {81 const value = source[prop];82 if (typeof value === "number" || typeof value === "string") {83 const displayHeight = validateDisplayHeight(parseInt(value));84 if (displayHeight) info.displayHeight = displayHeight;85 }86 });87 if (typeof source.category === "string") info.category = source.category;88 if (source.tags instanceof Array) info.tags = source.tags;89 switch (typeof source.palette) {90 case "boolean":91 info.palette = source.palette;92 break;93 case "string":94 switch (source.palette.toLowerCase()) {95 case "colorless":96 case "false":97 info.palette = false;98 break;99 case "colorful":100 case "true": info.palette = true;101 }102 break;103 }104 if (source.hidden === true) info.hidden = true;105 Object.keys(source).forEach((key) => {106 const value = source[key];107 if (typeof value !== "string") return;108 switch (key) {109 case "url":110 case "uri":111 info.author.url = value;112 break;113 case "licenseURL":114 case "licenseURI":115 info.license.url = value;116 break;117 case "licenseID":118 case "licenseSPDX":119 info.license.spdx = value;120 break;121 }122 });123 return info;124}125export { convertIconSetInfo };126 