<commit_before>"use strict";

var Promise = require('bluebird');
var extend = require('extend');
var defaults = extend(Object.create(null), require('./defaults.json'));

var request = Promise.promisify(require('request'));
var tokenUpdateTime = 12 * 60 * 1000;

/**
 * Check if specific required request parameter is valid
 */
function checkRequiredParameter (param) {
  return (
    typeof param === 'function' || typeof param === 'undefined' || param === '' || param === null || (typeof param === 'number' && isNaN(param))
  );
}

/**
 * Return a promise with normalized request parameters or an error
 */
function normalizeRequestParameters (defaultsObject, paramsName, requestParams) {
  var promise = new Promise(function (resolve, reject) {
    var defaultParams;
  
    if (typeof defaultsObject[paramsName] !== 'object') reject(new Error(`No default parameters for '${paramsName}'`));
    if (typeof requestParams !== 'object') reject(new Error('No request parameters provided'));
    
    defaultParams = extend(Object.create(null), defaultsObject[paramsName]);
    
    for (var key in defaultParams) {
      if (typeof requestParams[key] !== 'undefined') defaultParams[key] = requestParams[key];
      if (checkRequiredParameter(defaultParams[key])) reject(new Error(`Missing required parameter '${key}'`));
      if (defaultParams[key] === '__optional') defaultParams[key] = '';
    }
    
    resolve(defaultParams);
    <commit_msg>Add RequiredError class, change required parameters validation<commit_after>"use strict";

var Promise = require('bluebird');
var extend = require('extend');
var defaults = extend(Object.create(null), require('./defaults.json'));

var request = Promise.promisify(require('request'));
var tokenUpdateTime = 12 * 60 * 1000;

/**
 * Missing required parameter Error subclass
 */
class RequiredError extends Error {
  constructor(message, propertyName, propertyValue) {
    super(message);
    this.message = message;
    this.name = 'RequiredError';
    this.propertyName = propertyName;
    this.propertyValue = propertyValue;
  }
}

/**
 * Check if specific required request parameter is valid
 */
function checkRequiredParameter (param) {
  return (
    typeof param === 'function' || typeof param === 'undefined' || param === '' || param === null || (typeof param === 'number' && isNaN(param))
  );
}

/**
 * Return a promise with normalized request parameters or an error
 */
function normalizeRequestParameters (defaultsObject, paramsName, requestParams) {
  var promise = new Promise(function (resolve, reject) {
    var defaultParams;
  
    if (typeof defaultsObject[paramsName] !== 'object') reject(new RequiredError(`No default parameters for '${paramsName}'`, paramsName, defaultsObject[paramsName]));
    if (typeof requestParams !== 'object') reject(new RequiredError('No request parameters provided', 'requestParams', requestParams));
    
    defaultParams = extend(Object.create(null), defaultsObject[paramsName]);
    
    for (var key in defaultParams) {
      if (checkRequiredParameter(requestParams[key]) && defaultParams[key] === '') {
        reject(new RequiredError(`Missing required parameter '${key}'`, key, requestParams[key]));
      } else if (!checkRequiredParameter(requestParams[key])) {
        defaultParams[key] = requestParams[key];
      }
      if (defaultParams[key] === '__optional') defaultParams[key] = '';
    }
    
    resolve(defaultParams);
    
{'fraction_non_alphanumeric': 0.14052953156822812, 'fraction_numerical': 0.004655222577829503, 'mean_word_length': 4.673267326732673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '147312', 'n_tokens_mistral': 898, 'n_tokens_neox': 877, 'n_words': 259}