<commit_before>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2016
 */

/**
 * Sorts data by property.
 *
 * @param  {Realm.results}  data          The data to sort.
 * @param  {string}         sortBy        The property of the data to sort by.
 * @param  {string}         sortDataType  The type of the property, either:
 *
 *                                        'string' for alphabetical sorting (ASCII order for nums),
 *                                        'number' for numeric sorting (letters will error),
 *                                        'realm'  for default |realm.results.sorted()| sorting,
 *                                                 this is also the default sort (will error on
 *                                                 property of a property).
 *
 * @param   {boolean}        isAscending  If false, the sort order will be reversed.
 * @return  {object}                      The final sorted data.
 */
export const sortDataBy = (data, sortBy, sortDataType, isAscending = true) => {
  let sortedData;
  switch (sortDataType) {
    case 'string':
      sortedData = data.slice().sort((a, b) => a[sortBy].localeCompare(b[sortBy]));
      if (!isAscending) sortedData.reverse();
      break;
    case 'number':
      // Casts to number to cover cases where the property is a string (e.g. |serialNumber|).
      sortedData = data.slice().sort((a, b) => Number(a[sortBy]) - Number(b[sortBy]));
      if (!isAscending) sortedData.reverse();
      break;
    case 'realm':
    default:
      sortedData = data.sorted(sortBy, !isAscending);
  }
  return sortedData;
};

const sortKeyToType = {
  itemCode: 'string',
  itemName: 'string',
  availableQuantity: 'number',
  totalQuantity: 'number',
  expiryDate: 'date',
  serialNumber: 'number',
  numberOfItems: 'number',
  status: 'string',
  entryDate: 'date',
  otherPartyName: 'string',
  ourStockOnHand: 'number',
  monthlyUsage: 'number',
  suggestedQuantity: 'number',
  requiredQuantity: 'number',
  createdDate: 'date',
  name: 'string',
  code: 'string',
};

/**
 * Sorts an array of objects, returning a new array.
 * Sorts strings, numbers, dates or booleans.
 * @param  {Array}   data          Array of objects to sort
 * @param  {String}  sortKey       Key for the field to sort by
 * @param  {Boolean} isAscending   True if ascending, false otherwise.
 * @return {Array}   A new array of sorted data
 */
export const newSortDataBy = (data, sortKey, isAscending = true, sortDataType) => {
  const sortType = sortDataType || sortKeyToType[sortKey];

  switch (sortType) {
    case 'string':
      if (isAscending) return [...data.sort((a, b) => a[sortKey].localeCompare(b[sortKey]))];
      return [...data.sort((a, b) => b[sortKey].localeCompare(a[sortKey]))];
    case 'number':
      // Casts to number to cover cases where the property is a string (e.g. |serialNumber|).
      if (isAscending) return [...data.sort((a, b) => Number(a[sortKey]) - Number(b[sortKey]))];<commit_msg>Add  stocktake edit page sort keys<commit_after>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2016
 */

/**
 * Sorts data by property.
 *
 * @param  {Realm.results}  data          The data to sort.
 * @param  {string}         sortBy        The property of the data to sort by.
 * @param  {string}         sortDataType  The type of the property, either:
 *
 *                                        'string' for alphabetical sorting (ASCII order for nums),
 *                                        'number' for numeric sorting (letters will error),
 *                                        'realm'  for default |realm.results.sorted()| sorting,
 *                                                 this is also the default sort (will error on
 *                                                 property of a property).
 *
 * @param   {boolean}        isAscending  If false, the sort order will be reversed.
 * @return  {object}                      The final sorted data.
 */
export const sortDataBy = (data, sortBy, sortDataType, isAscending = true) => {
  let sortedData;
  switch (sortDataType) {
    case 'string':
      sortedData = data.slice().sort((a, b) => a[sortBy].localeCompare(b[sortBy]));
      if (!isAscending) sortedData.reverse();
      break;
    case 'number':
      // Casts to number to cover cases where the property is a string (e.g. |serialNumber|).
      sortedData = data.slice().sort((a, b) => Number(a[sortBy]) - Number(b[sortBy]));
      if (!isAscending) sortedData.reverse();
      break;
    case 'realm':
    default:
      sortedData = data.sorted(sortBy, !isAscending);
  }
  return sortedData;
};

const sortKeyToType = {
  itemCode: 'string',
  itemName: 'string',
  availableQuantity: 'number',
  totalQuantity: 'number',
  expiryDate: 'date',
  serialNumber: 'number',
  numberOfItems: 'number',
  status: 'string',
  entryDate: 'date',
  otherPartyName: 'string',
  ourStockOnHand: 'number',
  monthlyUsage: 'number',
  suggestedQuantity: 'number',
  requiredQuantity: 'number',
  createdDate: 'date',
  name: 'string',
  code: 'string',
  snapshotTotalQuantity: 'number',
  countedTotalQuantity: 'number',
  difference: 'number',
};

/**
 * Sorts an array of objects, returning a new array.
 * Sorts strings, numbers, dates or booleans.
 * @param  {Array}   data          Array of objects to sort
 * @param  {String}  sortKey       Key for the field to sort by
 * @param  {Boolean} isAscending   True if ascending, false otherwise.
 * @return {Array}   A new array of sorted data
 */
export const newSortDataBy = (data, sortKey, isAscending = true, sortDataType) => {
  const sortType = sortDataType || sortKeyToType[sortKey];

  switch (sortType) {
    case 'string':
      if (isAscending) return [...data.sort((a, b) => a[sortKey].localeCompare(b[sortKey]))];
      return [...data.sort((a, b) => b[sortKey].localeCompare(a[sortKey]))];
    case 'number':
      // Casts to number to cover cases where the property is a string (e.g. |serialNumber|).
      if (isAscending) return [...data.sort((a, b) => Number(a[sortKey]) - Number(b[sortKey]))];
{'fraction_non_alphanumeric': 0.12844940867279894, 'fraction_numerical': 0.001314060446780552, 'mean_word_length': 2.4814179531160665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1333288', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1776, 'n_words': 566}