<commit_before>/** Ben (OutdatedVersion) | May/10/2017 (4:45 PM) */

import { InvalidArgumentError, PreconditionFailedError } from 'restify'

const combinedPattern = new RegExp( [ /A-Za-z0-9_{1,16}/,
                                      /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/,
                                      /[0-9a-f]{32}/ ].join('|'), 'i')


/**
 * Middleware item for checking whether
 * the requested item does in fact match
 * the requirements for a 'key'.
 *
 * @param req - Request object
 * @param res - Response object
 * @param next - Cycle advancement
 */
export default (req, res, next) =>
{
    const id = req.params.id

    if (id === '')
    {
        res.send(new InvalidArgumentError('No name/UUID was found on your request'))
        return
    }

    if (!id.match(combinedPattern))
    {
        res.send(new PreconditionFailedError(`'${id}' did not match any valid pattern! [undashed UUID, dashed UUID, username]`))
        return
    }

    // everything checks out, continue request cycle
    next()
}<commit_msg>fix regexes to actually work<commit_after>/** Ben (OutdatedVersion) | May/10/2017 (4:45 PM) */

import { InvalidArgumentError, PreconditionFailedError } from 'restify'

const allPatterns = [ /[A-Za-z0-9_]{1,16}$/,
                      /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
                      /[0-9a-f]{32}$/ ]


/**
 * Middleware item for checking whether
 * the requested item does in fact match
 * the requirements for a 'key'.
 *
 * @param req - Request object
 * @param res - Response object
 * @param next - Cycle advancement
 */
export default (req, res, next) =>
{
    const id = req.params.id

    if (id === '')
    {
        res.send(new InvalidArgumentError('No name/UUID was found on your request'))
        return
    }


    let err = {}

    for (let pattern of allPatterns)
    {
        if (id.match(pattern))
        {
            break
        }
        else
        {
            err.yes = pattern
        }
    }


    if (err.yes)
    {
        res.send(new PreconditionFailedError(`'${id}' did not match a required pattern! [${err.yes}]`))
        return
    }

    // everything checks out, continue request cycle
    next()
}
{'fraction_non_alphanumeric': 0.1543594306049822, 'fraction_numerical': 0.030249110320284697, 'mean_word_length': 2.097796143250689, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3819719', 'n_tokens_mistral': 764, 'n_tokens_neox': 722, 'n_words': 191}