<commit_before>/*
 * Copyright 2014, Mozilla Foundation and contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

var Promise = require('../util/promise').Promise;
var l10n = require('../util/l10n');
var Conversion = require('./types').Conversion;
var Status = require('./types').Status;

exports.items = [
  {
    // The union type allows for a combination of different parameter types.
    item: 'type',
    name: 'union',

    constructor: function() {
      // Get the properties of the type. Later types in the list should always
      // be more general, so 'catch all' types like string must be last
      this.types = this.types.map(function(typeData) {
        return this.types.createType(typeData);
      }.bind(this));
    },

    stringify: function(value, context) {
      if (value == null) {
        return '';
      }

      var type = this.types.find(function(typeData) {
        return typeData.name === value.type;
      });

      return type.stringify(value[value.type], context);
    },

    parse: function(arg, context) {
      // Try to parse the given argument in the provided order of the parameter
      // types. Returns a promise containing the Conversion of the value that
      // was parsed.
      var self = this;

      var onError = function(i) {
        if (i >= self.types.length) {
          return Promise.reject(new Conversion(undefined, arg, Status.ERROR,
            l10n.lookup("commandParseError")));
        }
        else {
          return tryNext(i + 1);
        }
      };

      var tryNext = function(i) {
        var type = self.types[i];

        try {
          return type.parse(arg, context).then(function(conversion) {
            if (conversion.getStatus() === Status.VALID ||
                conversion.getStatus() === Status.INCOMPLETE) {
              // Converts the conversion value of the union type to an
              // object that identifies the current working type and the
              // data associated with it
              if (conversion.value) {
                var oldConversionValue = conversion.value;
                conversion.value = { type: type.name };
                conversion.value[type.name] = oldConversionValue;
              }

              return conversion;
            }
            else {
              return onError(i);
            }
          });
        }
        catch (ex) {
          return onError(i);
        }
      };

      return Promise.resolve(tryNext(0));
    },
  }
];<commit_msg>union-1016578: Use bind rather than self<commit_after>/*
 * Copyright 2014, Mozilla Foundation and contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

var Promise = require('../util/promise').Promise;
var l10n = require('../util/l10n');
var Conversion = require('./types').Conversion;
var Status = require('./types').Status;

exports.items = [
  {
    // The union type allows for a combination of different parameter types.
    item: 'type',
    name: 'union',

    constructor: function() {
      // Get the properties of the type. Later types in the list should always
      // be more general, so 'catch all' types like string must be last
      this.types = this.types.map(function(typeData) {
        return this.types.createType(typeData);
      }.bind(this));
    },

    stringify: function(value, context) {
      if (value == null) {
        return '';
      }

      var type = this.types.find(function(typeData) {
        return typeData.name === value.type;
      });

      return type.stringify(value[value.type], context);
    },

    parse: function(arg, context) {
      // Try to parse the given argument in the provided order of the parameter
      // types. Returns a promise containing the Conversion of the value that
      // was parsed.

      var onError = function(i) {
        if (i >= this.types.length) {
          return Promise.reject(new Conversion(undefined, arg, Status.ERROR,
            l10n.lookup("commandParseError")));
        }
        else {
          return tryNext(i + 1);
        }
      }.bind(this);

      var tryNext = function(i) {
        var type = this.types[i];

        try {
          return type.parse(arg, context).then(function(conversion) {
            if (conversion.getStatus() === Status.VALID ||
                conversion.getStatus() === Status.INCOMPLETE) {
              // Converts the conversion value of the union type to an
              // object that identifies the current working type and the
              // data associated with it
              if (conversion.value) {
                var oldConversionValue = conversion.value;
                conversion.value = { type: type.name };
                conversion.value[type.name] = oldConversionValue;
              }

              return conversion;
            }
            else {
              return onError(i);
            }
          });
        }
        catch (ex) {
          return onError(i);
        }
      }.bind(this);

      return Promise.resolve(tryNext(0));
    },
  }
];
{'fraction_non_alphanumeric': 0.10650887573964497, 'fraction_numerical': 0.00641025641025641, 'mean_word_length': 2.1157194060419866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '320312', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1555, 'n_words': 544}