Changeset 7174 in josm for trunk/data


Ignore:
Timestamp:
2014-05-24T01:47:59+02:00 (10 years ago)
Author:
Don-vip
Message:

update opening_hours.js

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/data/validator/opening_hours.js

    r7046 r7174  
    18421842                // console.log(JSON.stringify(tokens, null, '\t'));
    18431843                var prettified_value = '';
    1844                 var used_subparsers = {}; // Used sub parsers for one block, will be reset for each block. Declared as global, because it is manipulation inside various sub parsers.
     1844                var used_subparsers = {}; // Used sub parsers for one block, will be reset for each block. Declared in global namespace, because it is manipulation inside various sub parsers.
    18451845                var week_stable = true;
    18461846
     
    19051905                                blocks.push(selectors);
    19061906
    1907                                 // this handles selectors with time ranges wrapping over midnight (e.g. 10:00-02:00)
    1908                                 // it generates wrappers for all selectors and creates a new block
     1907                                // This handles selectors with time ranges wrapping over midnight (e.g. 10:00-02:00)
     1908                                // it generates wrappers for all selectors and creates a new block.
    19091909                                if (selectors.wraptime.length > 0) {
    19101910                                        var wrapselectors = {
     
    19341934                // }}}
    19351935
    1936                 // Tokenization function: Splits string into parts. {{{
    1937                 // output: array of arrays of pairs [content, type]
     1936                /* Tokenization function: Splits string into parts. {{{
     1937                 *
     1938                 * :param value: Raw opening_hours value.
     1939                 * :returns: Tokenized list object. Complex structure. You can print the
     1940                 *              thing as JSON if you would like to know in details.
     1941                 *              The most inner list has the following items: [ internal_value, token_name, value_length ].
     1942                 */
    19381943                function tokenize(value) {
    1939                         var all_tokens       = new Array();
     1944                        var all_tokens        = new Array();
    19401945                        var curr_block_tokens = new Array();
    19411946
     
    19441949                        while (value != '') {
    19451950                                var tmp;
    1946                                 if (tmp = value.match(/^(?:week\b|open\b|unknown)/i)) {
    1947                                         // reserved word
     1951                                if (tmp = value.match(/^(?:week\b|open\b|unknown\b)/i)) {
     1952                                        // Reserved keywords.
    19481953                                        curr_block_tokens.push([tmp[0].toLowerCase(), tmp[0].toLowerCase(), value.length ]);
    19491954                                        value = value.substr(tmp[0].length);
    19501955                                } else if (tmp = value.match(/^24\/7/i)) {
    1951                                         // reserved word
     1956                                        // Reserved keyword.
    19521957                                        has_token[tmp[0]] = true;
    19531958                                        curr_block_tokens.push([tmp[0], tmp[0], value.length ]);
    19541959                                        value = value.substr(tmp[0].length);
    19551960                                } else if (tmp = value.match(/^(?:off|closed)/i)) {
    1956                                         // reserved word
     1961                                        // Reserved keywords.
    19571962                                        curr_block_tokens.push([tmp[0].toLowerCase(), 'closed', value.length ]);
    19581963                                        value = value.substr(tmp[0].length);
     
    19651970                                        value = value.substr(tmp[0].length);
    19661971                                } else if (tmp = value.match(/^(&|_|→|–|−|=|opening_hours=|ー|\?|~|~|:|°°|25x7|7[ ]?days( a week|)|all days?|every day|-late|public holidays?|7j?\/7|every day|до|рм|ам|jours fériés|sonn-|[a-zäößàáéøčěíúýřПнВсо]+\b)\.?/i)) {
    1967                                         // Handle all remaining words with error tolerance
     1972                                        // Handle all remaining words with error tolerance.
    19681973                                        var correct_val = returnCorrectWordOrToken(tmp[1].toLowerCase(), value.length);
    19691974                                        if (typeof correct_val == 'object') {
     
    19711976                                                value = value.substr(tmp[0].length);
    19721977                                        } else if (typeof correct_val == 'string') {
     1978                                                if (tmp[1].toLowerCase() == 'pm') {
     1979                                                        var hours_token_at = curr_block_tokens.length - 3;
     1980                                                        if (hours_token_at > 0) {
     1981                                                                if (matchTokens(curr_block_tokens, hours_token_at,
     1982                                                                                        'number', 'timesep', 'number')
     1983                                                                                ) {
     1984                                                                        var hours_token = curr_block_tokens[hours_token_at];
     1985                                                                } else if (matchTokens(curr_block_tokens, hours_token_at + 2,
     1986                                                                                        'number')
     1987                                                                                ) {
     1988                                                                        hours_token_at += 2;
     1989                                                                        var hours_token = curr_block_tokens[hours_token_at];
     1990                                                                }
     1991                                                                if (hours_token[0] <= 12) {
     1992                                                                        hours_token[0] += 12;
     1993                                                                        curr_block_tokens[hours_token_at] = hours_token;
     1994                                                                }
     1995                                                        }
     1996                                                }
    19731997                                                value = correct_val + value.substr(tmp[0].length);
    19741998                                        } else {
     
    19792003                                } else if (tmp = value.match(/^\d+/)) {
    19802004                                        // number
    1981                                         if (tmp[0] > 1900) // assumed to be a year number
     2005                                        if (tmp[0] > 1900) // Assumed to be a year number.
    19822006                                                curr_block_tokens.push([tmp[0], 'year', value.length ]);
    19832007                                        else
     
    20322056                // }}}
    20332057
    2034                 // error correction/tolerance function {{{
    2035                 // Go through word_error_correction hash and get correct value back.
     2058                /* error correction/tolerance function {{{
     2059                 * Go through word_error_correction hash and get correct value back.
     2060                 *
     2061                 * :param word: Wrong Word or character.
     2062                 * :param value_length: Current value_length (used for warnings).
     2063                 * :returns:
     2064                 *              * (valid) opening_hours sub string.
     2065                 *              * object with [ internal_value, token_name ] if value is correct.
     2066                 *              * undefined if word could not be found (and thus not be corrected).
     2067                 */
    20362068                function returnCorrectWordOrToken(word, value_length) {
    20372069                        for (var token_name in word_error_correction) {
     
    20402072                                                if (old_val == word) {
    20412073                                                        var val = word_error_correction[token_name][comment][old_val];
    2042                                                         if (token_name == 'wrong_words' && !done_with_warnings) {
     2074                                                        if (comment == 'default') {
     2075                                                                // Return internal representation of word.
     2076                                                                return [ val, token_name ];
     2077                                                        } else if (token_name == 'wrong_words' && !done_with_warnings) {
     2078                                                                // Replace wrong words or characters with correct ones.
     2079                                                                // This will return a string which is then being tokenized.
    20432080                                                                parsing_warnings.push([ -1, value_length - old_val.length,
    20442081                                                                        comment.replace(/<ko>/, old_val).replace(/<ok>/, val) ]);
    20452082                                                                return val;
    2046                                                         } else if (comment != 'default'){
     2083                                                        } else {
     2084                                                                // Get correct string value from the 'default' hash and generate warning.
    20472085                                                                var correct_abbr;
    20482086                                                                for (correct_abbr in word_error_correction[token_name]['default']) {
    20492087                                                                        if (word_error_correction[token_name]['default'][correct_abbr] == val)
    2050                                                                                 break; // FIXME
     2088                                                                                break;
    20512089                                                                }
    2052                                                                 // FIXME
    2053                                                                 if (token_name != 'timevar') { // normally written in lower case
    2054                                                                         correct_abbr = correct_abbr.charAt(0).toUpperCase() + correct_abbr.slice(1);
     2090                                                                if (typeof correct_abbr == 'undefined') {
     2091                                                                        throw 'Please file a bug for opening_hours.js.'
     2092                                                                                + ' Including the stacktrace.'
     2093                                                                }
     2094                                                                if (token_name != 'timevar') {
     2095                                                                        // Everything else than timevar:
     2096                                                                        // E.g. 'Mo' are start with a upper case letter.
     2097                                                                        // It just looks better.
     2098                                                                        correct_abbr = correct_abbr.charAt(0).toUpperCase()
     2099                                                                                + correct_abbr.slice(1);
    20552100                                                                }
    20562101                                                                if (!done_with_warnings)
    20572102                                                                        parsing_warnings.push([ -1, value_length - old_val.length,
    20582103                                                                                comment.replace(/<ko>/, old_val).replace(/<ok>/, correct_abbr) ]);
     2104                                                                return [ val, token_name ];
    20592105                                                        }
    2060                                                         return [ val, token_name ];
    20612106                                                }
    20622107                                        }
    20632108                                }
    20642109                        }
     2110                        return undefined;
    20652111                }
    20662112                // }}}
    20672113
    2068                 // return warnings as list {{{
     2114                /* return warnings as list {{{
     2115                 *
     2116                 * :param it: Iterator object if available (optional).
     2117                 * :returns: Warnings as list with one warning per element.
     2118                 */
    20692119                function getWarnings(it) {
    20702120                        if (typeof it == 'object') { // getWarnings was called in a state without critical errors. We can do extended tests.
     
    20902140                // }}}
    20912141
    2092                 // Function to check token array for specific pattern {{{
     2142                /* Function to check token array for specific pattern {{{
     2143                 *
     2144                 * :param tokens: List of token objects.
     2145                 * :param at: Position at which the matching should begin.
     2146                 * :param token_name(s): One or many token_name strings which have to match in that order.
     2147                 * :returns: true if token_name(s) match in order false otherwise.
     2148                 */
    20932149                function matchTokens(tokens, at /*, matches... */) {
    20942150                        if (at + arguments.length - 2 > tokens.length)
     
    21032159                // }}}
    21042160
    2105                 // Generate selector wrapper with time offset {{{
     2161                /* Generate selector wrapper with time offset {{{
     2162                 *
     2163                 * :param func: Generated selector code function.
     2164                 * :param shirt: Time to shift in milliseconds.
     2165                 * :param token_name(s): One or many token_name strings which have to match in that order.
     2166                 * :returns: See selector code.
     2167                 */
    21062168                function generateDateShifter(func, shift) {
    21072169                        return function(date) {
     
    21152177                // }}}
    21162178
    2117                 // Top-level parser {{{
     2179                /* Top-level parser {{{
     2180                 *
     2181                 * :param tokens: List of token objects.
     2182                 * :param at: Position at which the matching should begin.
     2183                 * :param selectors: Reference to selector object.
     2184                 * :param nblock: Block number starting with 0.
     2185                 * :param conf: Configuration for prettifyValue.
     2186                 * :returns: See selector code.
     2187                 */
    21182188                function parseGroup(tokens, at, selectors, nblock, conf) {
    21192189                        var prettified_group_value = '';
     
    22382308
    22392309                                if (typeof conf != 'undefined') {
    2240 
    22412310                                        // 'Mo: 12:00-13:00' -> 'Mo 12:00-13:00'
    22422311                                        if (used_subparsers['time ranges'] && old_at > 1 && tokens[old_at-1][0] == ':'
     
    22842353
    22852354                // helper functions for sub parser {{{
    2286                 // for given date, returns date moved to the start of specified day minute
     2355
     2356                /* For given date, returns date moved to the start of the day with an offset specified in minutes. {{{
     2357                 * For example, if date is 2014-05-19_18:17:12, dateAtDayMinutes would
     2358                 * return 2014-05-19_02:00:00 for minutes=120.
     2359                 *
     2360                 * :param date: Date object.
     2361                 * :param minutes: Minutes used as offset starting from midnight of current day.
     2362                 * :returns: Moved date object.
     2363                 */
    22872364                function dateAtDayMinutes(date, minutes) {
    22882365                        return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, minutes);
    22892366                }
    2290 
    2291                 // for given date, returns date moved to the specific day of week
     2367                // }}}
     2368
     2369                /* For given date, returns date moved to the specific day of week {{{
     2370                 *
     2371                 * :param date: Date object.
     2372                 * :param day: Integer number for day of week. Starting with zero (Sunday).
     2373                 * :returns: Moved date object.
     2374                 */
    22922375                function dateAtNextWeekday(date, day) {
    22932376                        var delta = day - date.getDay();
    22942377                        return new Date(date.getFullYear(), date.getMonth(), date.getDate() + delta + (delta < 0 ? 7 : 0));
    22952378                }
    2296 
     2379                // }}}
     2380
     2381                /* Function to determine whether an array contains a value {{{
     2382                 * Source: http://stackoverflow.com/a/1181586
     2383                 *
     2384                 * :param needle: Element to find.
     2385                 * :returns: Index of element if present, if not present returns -1.
     2386                 */
    22972387                function indexOf(needle) {
    22982388                        if(typeof Array.prototype.indexOf === 'function') {
     
    23122402                        return indexOf.call(this, needle);
    23132403                }
    2314 
    2315                 // Numeric list parser (1,2,3-4,-1), used in weekday parser above
     2404                // }}}
     2405
     2406                /* Numeric list parser (1,2,3-4,-1) {{{
     2407                 * Used in weekday parser above.
     2408                 *
     2409                 * :param tokens: List of token objects.
     2410                 * :param at: Position where to start.
     2411                 * :param func: Function func(from, to, at).
     2412                 * :returns: Position at which the token does not belong to the list any more.
     2413                 */
    23162414                function parseNumRange(tokens, at, func) {
    23172415                        for (; at < tokens.length; at++) {
     
    23392437                        return at;
    23402438                }
    2341 
     2439                // }}}
     2440
     2441                /* List parser for constrained weekdays in month range {{{
     2442                 * e.g. Su[-1] which selects the last Sunday of the month.
     2443                 *
     2444                 * :param tokens: List of token objects.
     2445                 * :param at: Position where to start.
     2446                 * :returns: Array:
     2447                 *                      0. Constrained weekday number.
     2448                 *                      1. Position at which the token does not belong to the list any more (after ']' token).
     2449                 */
    23422450                function getConstrainedWeekday(tokens, at) {
    23432451                        var number = 0;
     
    23522460                                        if (number != 0)
    23532461                                                throw formatWarnErrorMessage(nblock, at,
    2354                                                         'You can not use a more than one constrained weekday in a month range');
     2462                                                        'You can not use more than one constrained weekday in a month range');
    23552463                                        number = from;
    23562464                                } else {
     
    23652473                        return [ number, endat + 1 ];
    23662474                }
     2475                // }}}
    23672476
    23682477                // Check if period is ok. Period 0 or 1 don’t make much sense.
     2478                /* List parser for constrained weekdays in month range {{{
     2479                 * e.g. Su[-1] which selects the last Sunday of the month.
     2480                 *
     2481                 * :param tokens: List of token objects.
     2482                 * :param at: Position where to start.
     2483                 * :returns: Array:
     2484                 *                      0. Constrained weekday number.
     2485                 *                      1. Position at which the token does not belong to the list any more (after ']' token).
     2486                 */
    23692487                function checkPeriod(at, period, period_type, parm_string) {
    23702488                        if (done_with_warnings)
     
    39804098                // }}}
    39814099
    3982                 // generate prettified value based on tokens {{{
     4100                // Generate prettified value based on tokens {{{
    39834101                function prettifySelector(tokens, at, last_at, conf, used_parseTimeRange) {
    39844102                        var value = '';
     
    39934111                                        }
    39944112                                        value += weekdays[tokens[at][0]];
    3995                                 } else if (at - start_at > 0 && used_parseTimeRange > 0 && matchTokens(tokens, at-1, 'timesep')
    3996                                                 && matchTokens(tokens, at, 'number')) { // '09:0' -> '09:00'
     4113                                } else if (at - start_at > 0 // e.g. '09:0' -> '09:00'
     4114                                                && used_parseTimeRange > 0
     4115                                                && matchTokens(tokens, at-1, 'timesep')
     4116                                                && matchTokens(tokens, at, 'number')) {
    39974117                                        value += (tokens[at][0] < 10 ? '0' : '') + tokens[at][0].toString();
    3998                                 } else if (used_parseTimeRange > 0 && conf.leading_zero_hour && at != tokens.length
     4118                                } else if (used_parseTimeRange > 0 // e.g. '9:00' -> ' 09:00'
     4119                                                && conf.leading_zero_hour
     4120                                                && at != tokens.length
    39994121                                                && matchTokens(tokens, at, 'number')
    4000                                                 && matchTokens(tokens, at+1, 'timesep')) { // '9:00' -> '19:00'
    4001                                         value += (tokens[at][0] < 10 ? (tokens[at][0] == 0 && conf.one_zero_if_hour_zero ? '' : '0') : '') + tokens[at][0].toString();
    4002                                 } else if (used_parseTimeRange > 0 && at + 2 < last_at
     4122                                                && matchTokens(tokens, at+1, 'timesep')) {
     4123                                        value += (
     4124                                                        tokens[at][0] < 10 ?
     4125                                                                (tokens[at][0] == 0 && conf.one_zero_if_hour_zero ?
     4126                                                                 '' : '0') :
     4127                                                                '') + tokens[at][0].toString();
     4128                                } else if (used_parseTimeRange > 0 // e.g. '9-18' -> '09:00-18:00'
     4129                                                && at + 2 < last_at
    40034130                                                && matchTokens(tokens, at, 'number')
    40044131                                                && matchTokens(tokens, at+1, '-')
    4005                                                 && matchTokens(tokens, at+2, 'number')) { // '9-18' -> '09:00-18:00'
    4006                                         value += (tokens[at][0] < 10 ? (tokens[at][0] == 0 && conf.one_zero_if_hour_zero ? '' : '0') : '') + tokens[at][0].toString();
    4007                                         value += ':00-';
    4008                                         value += (tokens[at+2][0] < 10 ? '0' : '') + tokens[at+2][0].toString();
    4009                                         value += ':00';
     4132                                                && matchTokens(tokens, at+2, 'number')) {
     4133                                        value += (tokens[at][0] < 10 ?
     4134                                                        (tokens[at][0] == 0 && conf.one_zero_if_hour_zero ? '' : '0')
     4135                                                        : '') + tokens[at][0].toString();
     4136                                        value += ':00-'
     4137                                                + (tokens[at+2][0] < 10 ? '0' : '') + tokens[at+2][0].toString()
     4138                                                + ':00';
    40104139                                        at += 2;
    40114140                                } else if (matchTokens(tokens, at, 'comment')) {
     
    43584487        }
    43594488}));
     4489// vim: set ts=4 sw=4 tw=0 noet foldmarker={{{,}}} foldlevel=0 foldmethod=marker :
Note: See TracChangeset for help on using the changeset viewer.