all files / scour/lib/ build_extensions.js

100% Statements 7/7
100% Branches 2/2
100% Functions 1/1
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                    11× 11× 15×             15× 15×      
'use strict'
 
const map = require('../utilities/map')
 
/**
 * Internal: builds extensions based on parameters passed onto `.use()`.
 *
 *     buildExtensions({ 'users.*': props })
 *     // => [ /^users\.[^.]+$/, props ]
 */
 
module.exports = function buildExtensions (keypath, extensions) {
  let prefix = keypath.length ? (keypath.join('.') + '.') : ''
  return map(extensions, (properties, keypath) => {
    keypath = (prefix + keypath)
      .replace(/\./g, '\\.')
      .replace(/\*\*/g, '::all::')
      .replace(/\*/g, '::any::')
      .replace(/::all::/g, '.*')
      .replace(/::any::/g, '[^\.]+')
 
    keypath = new RegExp('^' + keypath + '$')
    return [ keypath, properties ]
  })
}