<commit_before>import Ember from 'ember';

var assert = Ember.assert;
var get = Ember.get;
var compare = Ember.compare;
var computed = Ember.computed;
var isArray = Ember.isArray;


/**
  A computed property which returns a new sorted array of content from the
  first dependent array. The sort order is based on the string or array of string values
  provided as the second argument.

  The second argument, sortDefinitions, can be a string or array of string value(s)
  which indicates the item property and direction of the sort. Adding a suffix of ':desc'
  will cause the list to be sorted in descending order.

  Example:

  ```javascript
  var ToDoList = Ember.Object.extend({
    // using standard ascending sort
    sortedTodos: Ember.computed.sortBy('todos', 'name'),

    // using descending sort
    sortedTodosDesc: Ember.computed.sortBy('todos', 'name:desc'),

    // using secondary sort
    sortedPriority: Ember.computed.sortBy('todos', ['priority', 'name'])
  });

  var todoList = ToDoList.create({todos: [
    { name: 'Unit Test', priority: 2 },
    { name: 'Documentation', priority: 3 },
    { name: 'Integration Test', priority: 2 },
    { name: 'Release', priority: 1 }
  ]});

  todoList.get('sortedTodos');      // [{ name:'Documentation', priority:3 }, { name: 'Integration Test', priority: 2 }, { name:'Release', priority:1 }, { name:'Unit Test', priority:2 }]
  todoList.get('sortedTodosDesc');  // [{ name:'Unit Test', priority:2 }, { name:'Release', priority:1 }, { name: 'Integration Test', priority: 2 }, { name:'Documentation', priority:3 }]
  todoList.get('priorityTodos');    // [{ name:'Release', priority:1 }, { name: 'Integration Test', priority: 2 }, { name:'Unit Test', priority:2 }, { name:'Documentation', priority:3 }]
  ```

  @method sort
  @for Ember.computed
  @param {String} itemsKey
  @param {String or Array} sortDefinitions a string or array of strings whose value(s) are sort properties (add `:desc` to the arrays sort properties to sort descending)
  @return {Ember.ComputedProperty} computes a new sorted array based on the sort property array
  @public
*/
export default function(itemsKey, sortDefinitions) {
  if(typeof sortDefinitions === 'string') {
    sortDefinitions = Ember.A([sortDefinitions]);
  }

  // TODO: Should we validate the all array values
  assert('sortBy expects a string or array of strings for its second argument', isArray(sortDefinitions));

  // Normalize the sort properties
  var normalizedSort = sortDefinitions.map(p => {
    let [prop, direction] = p.split(':');
    direction = direction || 'asc';
    return [prop, direction];
  });

  // Map out the dependantKeys for the computed macro
  var args = normalizedSort.map(prop => {
    return `${itemsKey}.@each.${prop}`;
  });

  // Push in the actual sorting function
  args.push(function() {
    var items = itemsKey === '@this' ? this : get(this, itemsKey);
    if (items === null || typeof items !== 'object') { return Ember.A(); }

    return Ember.A(items.slice().sort((itemA, itemB) => {
      for (var i = 0; i < normalizedSort.length; ++i) {
        var [prop, direction] = normalizedSort[i];<commit_msg>Updated the macro to provide better argument type checking.<commit_after>import Ember from 'ember';

var assert = Ember.assert;
var get = Ember.get;
var compare = Ember.compare;
var computed = Ember.computed;
var isArray = Ember.isArray;


/**
  A computed property which returns a new sorted array of content from the
  first dependent array. The sort order is based on the string or array of string values
  provided as the second argument.

  The second argument, sortDefinitions, can be a string or array of string value(s)
  which indicates the item property and direction of the sort. Adding a suffix of ':desc'
  will cause the list to be sorted in descending order.

  Example:

  ```javascript
  var ToDoList = Ember.Object.extend({
    // using standard ascending sort
    sortedTodos: Ember.computed.sortBy('todos', 'name'),

    // using descending sort
    sortedTodosDesc: Ember.computed.sortBy('todos', 'name:desc'),

    // using secondary sort
    sortedPriority: Ember.computed.sortBy('todos', ['priority', 'name'])
  });

  var todoList = ToDoList.create({todos: [
    { name: 'Unit Test', priority: 2 },
    { name: 'Documentation', priority: 3 },
    { name: 'Integration Test', priority: 2 },
    { name: 'Release', priority: 1 }
  ]});

  todoList.get('sortedTodos');      // [{ name:'Documentation', priority:3 }, { name: 'Integration Test', priority: 2 }, { name:'Release', priority:1 }, { name:'Unit Test', priority:2 }]
  todoList.get('sortedTodosDesc');  // [{ name:'Unit Test', priority:2 }, { name:'Release', priority:1 }, { name: 'Integration Test', priority: 2 }, { name:'Documentation', priority:3 }]
  todoList.get('priorityTodos');    // [{ name:'Release', priority:1 }, { name: 'Integration Test', priority: 2 }, { name:'Unit Test', priority:2 }, { name:'Documentation', priority:3 }]
  ```

  @method sort
  @for Ember.computed
  @param {String} itemsKey
  @param {String or Array} sortDefinitions a string or array of strings whose value(s) are sort properties (add `:desc` to the arrays sort properties to sort descending)
  @return {Ember.ComputedProperty} computes a new sorted array based on the sort property array
  @public
*/
export default function(itemsKey, sortDefinitions) {
  if(typeof sortDefinitions === 'string') {
    sortDefinitions = Ember.A([sortDefinitions]);
  }

  // Checks the sortDefinitions for correct value types.
  assert('Ember.computed.sortBy expects a string or array of strings as the sort definition.',
    isArray(sortDefinitions) && sortDefinitions.length > 0);
  sortDefinitions.forEach(s => {
    assert('Ember.computed.sortBy expects a string or array of strings as the sort definition.', typeof s === 'string');
  });

  // Normalize the sort properties
  var normalizedSort = sortDefinitions.map(p => {
    let [prop, direction] = p.split(':');
    direction = direction || 'asc';
    return [prop, direction];
  });

  // Map out the dependantKeys for the computed macro
  var args = normalizedSort.map(prop => {
    return `${itemsKey}.@each.${prop}`;
  });

  // Push in the actual sorting function
  args.push(function() {
    var items = itemsKey === '@this' ? this : get(this, itemsKey);
    if (items === null || typeof items !== 'object') { return Ember.A(); }

    return Ember.A(items.slice().sort((itemA, itemB) => {
      for (var i = 0; i < normalizedSort.length; ++i) {
        var [prop, direction] = normalizedSort[i];
{'fraction_non_alphanumeric': 0.1370906321401371, 'fraction_numerical': 0.005331302361005331, 'mean_word_length': 3.9146706586826348, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '201491', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1908, 'n_words': 662}