Skip to content

Commit

Permalink
feat(color-names): add new short export
Browse files Browse the repository at this point in the history
experimental
  • Loading branch information
meodai committed Aug 25, 2024
1 parent de0c1b5 commit 632973c
Show file tree
Hide file tree
Showing 12 changed files with 10,051 additions and 1 deletion.
1,246 changes: 1,246 additions & 0 deletions dist/colornames.short.csv

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/colornames.short.esm.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/colornames.short.esm.mjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/colornames.short.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/colornames.short.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/colornames.short.min.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/colornames.short.scss

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/colornames.short.umd.js

Large diffs are not rendered by default.

4,983 changes: 4,983 additions & 0 deletions dist/colornames.short.xml

Large diffs are not rendered by default.

3,735 changes: 3,735 additions & 0 deletions dist/colornames.short.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/history.json

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const fileNameSrc = 'colornames';
const fileNameBestOfPostfix = '.bestof';
const readmeFileName = 'README.md';

const fileNameShortPostfix = '.short';
const maxShortNameLength = 12;

const sortBy = 'name';
const csvKeys = ['name', 'hex'];
const bestOfKey = 'good name';
Expand Down Expand Up @@ -118,6 +121,24 @@ const JSONExportStringBestOf = JSON.stringify(
)
);

const JSONExportStringShort = JSON.stringify(
[...colorsSrc.entires]
.filter(
// make sure its only one word long
(val) =>
val[bestOfKey] &&
val.name.split(" ").length === 1 &&
val.name.length < maxShortNameLength
)
.map(
// removes good name attributes
(val) => ({
name: val.name,
hex: val.hex,
})
)
);

fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}.json`),
JSONExportString
Expand All @@ -128,6 +149,11 @@ fs.writeFileSync(
JSONExportStringBestOf
);

fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}${fileNameShortPostfix}.json`),
JSONExportStringShort
);

// creates a more compact JSON file, where the HEX color serves as an id
const miniJSONExportObj = colorsSrc.entires.reduce((obj, entry) => {
obj[entry.hex.replace('#', '')] = entry.name;
Expand All @@ -141,6 +167,17 @@ const miniJSONExportObjBestOf = colorsSrc.entires.reduce((obj, entry) => {
return obj;
}, {});

const miniJSONExportObjShort = colorsSrc.entires.reduce((obj, entry) => {
if (
entry[bestOfKey] &&
entry.name.split(" ").length === 1 &&
entry.name.length < maxShortNameLength
) {
obj[entry.hex.replace("#", "")] = entry.name;
}
return obj;
}, {});

fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}.min.json`),
JSON.stringify(miniJSONExportObj)
Expand All @@ -151,6 +188,11 @@ fs.writeFileSync(
JSON.stringify(miniJSONExportObjBestOf)
);

fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}${fileNameShortPostfix}.min.json`),
JSON.stringify(miniJSONExportObjShort)
);

// gets UMD template
const umdTpl = fs.readFileSync(
path.normalize(__dirname + '/umd.js.tpl'),
Expand All @@ -168,6 +210,11 @@ fs.writeFileSync(
umdTpl.replace('"{{COLORS}}"', JSONExportStringBestOf)
);

fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}${fileNameShortPostfix}.umd.js`),
umdTpl.replace('"{{COLORS}}"', JSONExportStringShort)
);

// gets ESM template
const esmTpl = fs.readFileSync(
path.normalize(__dirname + '/esm.js.tpl'),
Expand All @@ -193,6 +240,15 @@ fs.writeFileSync(
esmTpl.replace('"{{COLORS}}"', JSONExportStringBestOf)
);

fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}${fileNameShortPostfix}.esm.js`),
esmTpl.replace('"{{COLORS}}"', JSONExportStringShort)
);
fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}${fileNameShortPostfix}.esm.mjs`),
esmTpl.replace('"{{COLORS}}"', JSONExportStringShort)
);

// create foreign formats
// configuration for the file outputs
const outputFormats = {
Expand Down Expand Up @@ -264,6 +320,29 @@ for (const outputFormat in outputFormats) {
}
}

// short files
for (const outputFormat in outputFormats) {
if (outputFormats[outputFormat]) {
let outputString = objArrToString(
colorsSrc.entires.filter(
(val) =>
val[bestOfKey] &&
val.name.split(" ").length === 1 &&
val.name.length < maxShortNameLength
),
csvKeys,
outputFormats[outputFormat]
);
if (outputFormat === 'html' || outputFormat === 'xml') {
outputString = outputString.replace(/&/g, '&amp;');
}
fs.writeFileSync(
path.normalize(`${baseFolder}${folderDist}${fileNameSrc}${fileNameShortPostfix}.${outputFormat}`),
outputString
);
}
}

// updates the color count in readme file
const readme = fs.readFileSync(
path.normalize(`${baseFolder}${readmeFileName}`),
Expand Down

0 comments on commit 632973c

Please sign in to comment.