mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-09-29 04:39:04 +00:00
Allow gitcloud matching of icons for multiple words
This commit is contained in:
parent
4e7fe7fa55
commit
0c588b7219
@ -8,22 +8,21 @@ import helpers from './../helpers/helpers';
|
|||||||
const {downloadFile, allowedIconFormats} = helpers;
|
const {downloadFile, allowedIconFormats} = helpers;
|
||||||
tmp.setGracefulCleanup();
|
tmp.setGracefulCleanup();
|
||||||
|
|
||||||
|
const GITCLOUD_SPACE_DELIMITER = '-';
|
||||||
|
|
||||||
function inferIconFromStore(targetUrl, platform) {
|
function inferIconFromStore(targetUrl, platform) {
|
||||||
const allowedFormats = allowedIconFormats(platform);
|
const allowedFormats = allowedIconFormats(platform);
|
||||||
|
|
||||||
return gitCloud('http://jiahaog.com/nativefier-icons/')
|
return gitCloud('http://jiahaog.com/nativefier-icons/')
|
||||||
.then(fileIndex => {
|
.then(fileIndex => {
|
||||||
const matchingIcons = fileIndex
|
const iconWithScores = mapIconWithMatchScore(fileIndex, targetUrl);
|
||||||
.filter(item => {
|
const maxScore = getMaxMatchScore(iconWithScores);
|
||||||
// todo might have problems with matching length, e.g. `book` vs `facebook`
|
|
||||||
return targetUrl
|
if (maxScore === 0) {
|
||||||
.toLowerCase()
|
return null;
|
||||||
.includes(item.name);
|
}
|
||||||
})
|
|
||||||
.map(item => {
|
const matchingIcons = getMatchingIcons(iconWithScores, maxScore);
|
||||||
item.ext = path.extname(item.url);
|
|
||||||
return item;
|
|
||||||
});
|
|
||||||
|
|
||||||
let matchingUrl;
|
let matchingUrl;
|
||||||
for (let format of allowedFormats) {
|
for (let format of allowedFormats) {
|
||||||
@ -42,6 +41,52 @@ function inferIconFromStore(targetUrl, platform) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapIconWithMatchScore(fileIndex, targetUrl) {
|
||||||
|
const normalisedTargetUrl = targetUrl.toLowerCase();
|
||||||
|
return fileIndex
|
||||||
|
.map(item => {
|
||||||
|
const itemWords = item.name.split(GITCLOUD_SPACE_DELIMITER);
|
||||||
|
const score = itemWords.reduce((currentScore, word) => {
|
||||||
|
if (normalisedTargetUrl.includes(word)) {
|
||||||
|
return currentScore + 1;
|
||||||
|
}
|
||||||
|
return currentScore;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
return Object.assign({},
|
||||||
|
item,
|
||||||
|
{score}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMaxMatchScore(iconWithScores) {
|
||||||
|
return iconWithScores.reduce((maxScore, currentIcon) => {
|
||||||
|
const currentScore = currentIcon.score;
|
||||||
|
if (currentScore > maxScore) {
|
||||||
|
return currentScore;
|
||||||
|
}
|
||||||
|
return maxScore;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* also maps ext to icon object
|
||||||
|
*/
|
||||||
|
function getMatchingIcons(iconWithScores, maxScore) {
|
||||||
|
return iconWithScores
|
||||||
|
.filter(item => {
|
||||||
|
return item.score === maxScore;
|
||||||
|
})
|
||||||
|
.map(item => {
|
||||||
|
return Object.assign(
|
||||||
|
{},
|
||||||
|
item,
|
||||||
|
{ext: path.extname(item.url)}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function writeFilePromise(outPath, data) {
|
function writeFilePromise(outPath, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.writeFile(outPath, data, error => {
|
fs.writeFile(outPath, data, error => {
|
||||||
|
Loading…
Reference in New Issue
Block a user