Last active
March 18, 2016 06:08
-
-
Save thatkookooguy/211ef18bef110ea61337 to your computer and use it in GitHub Desktop.
Trying to create a fileName parser for kibibit. This should get the following data: 1) fileName (no extension) 2) extension 3) all `tags` (words after the 1st dot (excluding the extension and hidden file dot) 4) hidden state of file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var testFileName = 'kaki.js'; | |
var testHiddenFile = '.hidden'; | |
var testHiddenWithExtension = '.hidden.js'; | |
var testTags = 'kaki.test.js'; | |
var testHiddenWithTags = '.kaki.test.js'; | |
function analizeFileName(fileName) { | |
// input not a string | |
if(!angular.isString(fileName)) { | |
return {error: "fileName should be a string"}; | |
} | |
// input is invalid | |
if(fileName.split('.').join('').length === 0) { | |
return {error: "fileName is empty or invalid"}; | |
} | |
// INIT | |
var returnData = { | |
fileName: fileName | |
}; | |
var fileArray = fileName.split(/(\.)/g); | |
// GET ALL NEEDED DATA | |
returnData.isHidden = isHidden(fileArray); | |
fileArray = returnData.isHidden ? removeParsedParts(fileArray, true /* isHidden */) : fileArray; | |
returnData.fileExtension = getExtension(fileArray); | |
fileArray = returnData.fileExtension ? removeParsedParts(fileArray, false /* isHidden */, true /* hasExtension */) : fileArray; | |
returnData = angular.extend(returnData, getTags(fileArray)); | |
returnData.fileName = returnData.isHidden ? '.' + returnData.fileName : returnData.fileName; | |
return returnData; | |
} | |
function isHidden(fileArray) { | |
return fileArray[0] === '' && fileArray[1] === '.'; | |
} | |
function getExtension(fileName) { | |
// expects fileName without hidden part | |
var fileParts = file.split('.'); | |
if (fileParts.length > 2) { | |
return fileParts[fileParts.length - 1]; | |
} else { | |
// no extension | |
return false; | |
} | |
} | |
function getTags(fileArray) { | |
var onlyName = fileArray.join(''); | |
var splitByDot = onlyName.split('.'); | |
if (splitByDot.length < 2) { | |
return { fileName: onlyName, tags: [] }; | |
} | |
splitByDot.splice(0, 1); | |
return { fileName: onlyName, tags: splitByDot}; | |
} | |
function removeParsedParts(fileArray, isHidden, hasExtension) { | |
var _fileArray = angular.copy(fileArray); | |
if(isHidden) { | |
_fileArray.splice(0, 2); | |
} | |
if(hasExtension) { | |
_fileArray.splice(_fileArray.length - 2, 2); | |
} | |
return _fileArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Trying to create a fileName parser for kibibit.
This should get the following data:
tags
(words after the 1st dot (excluding the extension and hidden file dot)output should be an object: