diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Functions/dateFuncs.js | 23 | ||||
| -rw-r--r-- | Functions/open.js (renamed from open.js) | 0 | ||||
| -rw-r--r-- | Functions/stringFuncs.js | 51 | ||||
| -rw-r--r-- | server.js | 6 | 
5 files changed, 78 insertions, 5 deletions
@@ -1,5 +1,4 @@  Classes  node_modules  package-lock.json -shifts.txt -funcs
\ No newline at end of file +shifts.txt
\ No newline at end of file diff --git a/Functions/dateFuncs.js b/Functions/dateFuncs.js new file mode 100644 index 0000000..10c4250 --- /dev/null +++ b/Functions/dateFuncs.js @@ -0,0 +1,23 @@ +// s: DD-MM-YYYY, return: Date +function stringToDate(s) { +    const date = s.split("-"); +    return new Date(`${date[2].padStart(4, "0")}-${date[1].padStart(2, "0")}-${date[0].padStart(2, "0")}`); +} + +const isBetweenDates = (date, date1, date2) => { +    date = approxDate(date); +    date1 = approxDate(date1); +    date2 = approxDate(date2); +    return ((date.getTime() >= date1.getTime()) +    && (date.getTime() <= date2.getTime())); +}; + +function approxDate(d) +{ +    return new Date(`${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, "0")}-${d.getDate().toString().padStart(2, "0")}`); +} + +module.exports = { +    fromString: stringToDate, +    between: isBetweenDates +}
\ No newline at end of file diff --git a/open.js b/Functions/open.js index f04201b..f04201b 100644 --- a/open.js +++ b/Functions/open.js diff --git a/Functions/stringFuncs.js b/Functions/stringFuncs.js new file mode 100644 index 0000000..8dddc75 --- /dev/null +++ b/Functions/stringFuncs.js @@ -0,0 +1,51 @@ +function replaceElement(source, element, value) { +    const firstTag = `<${element}>`; + +    const firstSpace = element.indexOf(" "); +    if (firstSpace !== -1) +        element = element.substring(0, firstSpace); +    const startTag = `<${element}.*?>`; +    const endTag = `</${element}.*?>`; + +    const span = getTagSpan(source, [startTag, endTag], firstTag); + +    return source.substring(0, span[0]) + +        value + +        source.substring(span[1]); +} + +function getTagSpan(s, tags=["\\(", "\\)"], customFirstTag=undefined) +{ +    customFirstTag = new RegExp(customFirstTag || tags[0]); +    let i = s.search(customFirstTag); +    if (i === -1) +        return s; +     +    tags = [ +        new RegExp("^" + tags[0]), +        new RegExp("^" + tags[1]) +    ]; + +    const start = i; +    i++; +    let depth = 1; +    while((depth !== 0) && (i !== s.length)) +    { +        let maybeStartTag = s.substring(i); +        let maybeEndTag = s.substring(i); +         +        depth += +tags[0].test(maybeStartTag) + -tags[1].test(maybeEndTag); + +        i++; +    } +      +    i += (s.substring(i - 1).match(tags[1]) || [""])[0] .length - 1; +    return [start, i]; +} + +const countOccurrences = (s, regex) => (s.match(regex) || []).length; +module.exports = { +    span: getTagSpan, +    replaceElement: replaceElement, +    count: countOccurrences +}
\ No newline at end of file @@ -4,9 +4,9 @@ const url	= require("url");  const scrape	= require("./scrape.js");  const SQL_DBS	= require("./database.js");  const DBPARSE	= require("./dbparse.js"); -const openFile	= require("./open.js").file; -const strFuncs	= require("./funcs/stringFuncs.js"); -const dateFuncs	= require("./funcs/dateFuncs.js"); +const openFile	= require("./Functions/open.js").file; +const strFuncs	= require("./Functions/stringFuncs.js"); +const dateFuncs	= require("./Functions/dateFuncs.js");  async function init()  | 
