image-classifier-ts
Version:
Command line tool to auto-classify images, renaming them with appropriate labels. Uses Node and Google Vision API.
44 lines • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NameGeneratorUtil = void 0;
var StringUtils_1 = require("./StringUtils");
var NameGeneratorUtil;
(function (NameGeneratorUtil) {
function generateNameFromTokens(mapTokenToValue, format, description) {
validateFormat(format, description);
var newName = format;
mapTokenToValue.forEach(function (value, token) {
newName = StringUtils_1.StringUtils.replaceAll(newName, getTokenWithBraces(token), value);
});
if (hasTokenMarkers(newName)) {
throw new Error("New ".concat(description, " name is invalid - '").concat(newName, "'. Is the ").concat(description, " name format invalid? - '").concat(format, "'"));
}
return cleanNameForWindows(newName);
}
NameGeneratorUtil.generateNameFromTokens = generateNameFromTokens;
function cleanNameForWindows(name) {
var newName = name;
// Windows does not like a folder ending in '.'
// That can happen for the location part, if Google returns nothing for the last part (e.g. for area3)
if (newName.startsWith(".")) {
newName = newName.substr(1);
}
if (newName.endsWith(".")) {
newName = newName.substr(0, newName.length - 1);
}
return newName;
}
function getTokenWithBraces(token) {
return "{".concat(token, "}");
}
NameGeneratorUtil.getTokenWithBraces = getTokenWithBraces;
function validateFormat(format, description) {
if (!hasTokenMarkers(format)) {
throw new Error("Invalid ".concat(description, " name format: '").concat(format, "'"));
}
}
function hasTokenMarkers(format) {
return format.indexOf("{") > -1 || format.indexOf("}") > -1;
}
})(NameGeneratorUtil = exports.NameGeneratorUtil || (exports.NameGeneratorUtil = {}));
//# sourceMappingURL=NameGeneratorUtil.js.map