Data update
This commit is contained in:
parent
35bcdeebf8
commit
74c69a0df6
2427 changed files with 31826 additions and 3468 deletions
5
Task/Tokenize-a-string/JavaScript/tokenize-a-string-1.js
Normal file
5
Task/Tokenize-a-string/JavaScript/tokenize-a-string-1.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
console.log(
|
||||
"Hello,How,Are,You,Today"
|
||||
.split(",")
|
||||
.join(".")
|
||||
);
|
||||
74
Task/Tokenize-a-string/JavaScript/tokenize-a-string-2.js
Normal file
74
Task/Tokenize-a-string/JavaScript/tokenize-a-string-2.js
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
const Tokeniser = (function () {
|
||||
const numberRegex = /-?(\d+\.d+|\d+\.|\.\d+|\d+)((e|E)(\+|-)?\d+)?/g;
|
||||
return {
|
||||
settings: {
|
||||
operators: ["<", ">", "=", "+", "-", "*", "/", "?", "!"],
|
||||
separators: [",", ".", ";", ":", " ", "\t", "\n"],
|
||||
groupers: ["(", ")", "[", "]", "{", "}", '"', '"', "'", "'"],
|
||||
keepWhiteSpacesAsTokens: false,
|
||||
trimTokens: true
|
||||
},
|
||||
isNumber: function (value) {
|
||||
if (typeof value === "number") {
|
||||
return true;
|
||||
} else if (typeof value === "string") {
|
||||
return numberRegex.test(value);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
closeGrouper: function (grouper) {
|
||||
if (this.settings.groupers.includes(grouper)) {
|
||||
return this.settings.groupers[this.settings.groupers.indexOf(grouper) + 1];
|
||||
}
|
||||
return null;
|
||||
},
|
||||
tokenType: function (char) {
|
||||
if (this.settings.operators.includes(char)) {
|
||||
return "operator";
|
||||
} else if (this.settings.separators.includes(char)) {
|
||||
return "separator";
|
||||
} else if (this.settings.groupers.includes(char)) {
|
||||
return "grouper";
|
||||
}
|
||||
return "other";
|
||||
},
|
||||
parseString: function (str) {
|
||||
if (typeof str !== "string") {
|
||||
if (str === null) {
|
||||
return "null";
|
||||
} if (typeof str === "object") {
|
||||
str = JSON.stringify(str);
|
||||
} else {
|
||||
str = str.toString();
|
||||
}
|
||||
}
|
||||
let tokens = [], _tempToken = "";
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
if (this.tokenType(_tempToken) !== this.tokenType(str[i]) || this.tokenType(str[i]) === "separator") {
|
||||
if (_tempToken.trim() !== "") {
|
||||
tokens.push(this.settings.trimTokens ? _tempToken.trim() : _tempToken);
|
||||
} else if (this.settings.keepWhiteSpacesAsTokens) {
|
||||
tokens.push(_tempToken);
|
||||
}
|
||||
_tempToken = str[i];
|
||||
if (this.tokenType(_tempToken) === "separator") {
|
||||
if (_tempToken.trim() !== "") {
|
||||
tokens.push(this.settings.trimTokens ? _tempToken.trim() : _tempToken);
|
||||
} else if (this.settings.keepWhiteSpacesAsTokens) {
|
||||
tokens.push(_tempToken);
|
||||
}
|
||||
_tempToken = "";
|
||||
}
|
||||
} else {
|
||||
_tempToken += str[i];
|
||||
}
|
||||
}
|
||||
if (_tempToken.trim() !== "") {
|
||||
tokens.push(this.settings.trimTokens ? _tempToken.trim() : _tempToken);
|
||||
} else if (this.settings.keepWhiteSpacesAsTokens) {
|
||||
tokens.push(_tempToken);
|
||||
}
|
||||
return tokens.filter((token) => token !== "");
|
||||
}
|
||||
};
|
||||
})();
|
||||
3
Task/Tokenize-a-string/JavaScript/tokenize-a-string-3.js
Normal file
3
Task/Tokenize-a-string/JavaScript/tokenize-a-string-3.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Tokeniser.parseString("Hello,How,Are,You,Today");
|
||||
|
||||
// -> ['Hello', ',', 'How', ',', 'Are', ',', 'You', ',', 'Today']
|
||||
|
|
@ -1 +0,0 @@
|
|||
alert( "Hello,How,Are,You,Today".split(",").join(".") );
|
||||
Loading…
Add table
Add a link
Reference in a new issue