Hone logo
Hone
Problems

Intelligent Ordering with Jest

This challenge focuses on creating a function that intelligently orders a list of items based on a provided priority scheme. The function should handle various data types and prioritize items according to a user-defined ordering strategy, ensuring flexibility and robustness. This is a common requirement in many applications, from displaying notifications to sorting tasks in a project management tool.

Problem Description

You are tasked with creating a TypeScript function called intelligentOrder that takes an array of items and an ordering function as input. The intelligentOrder function should return a new array containing the items sorted according to the provided ordering function. The ordering function will receive an item and return a numerical priority value. Lower values indicate higher priority (i.e., should appear earlier in the sorted array).

Key Requirements:

  • Input:
    • items: An array of any type (any[]). This array can contain a mix of different data types (numbers, strings, objects, etc.).
    • orderingFunction: A function that accepts a single argument (an item from the items array) and returns a number representing its priority.
  • Output: A new array containing the items from the input array, sorted in ascending order based on the priority values returned by the orderingFunction.
  • Immutability: The original items array should not be modified. The function must return a new sorted array.
  • Error Handling: The orderingFunction might throw an error if it cannot determine the priority of an item. Your intelligentOrder function should gracefully handle these errors by assigning a default priority of Infinity to such items, ensuring they appear at the end of the sorted array.

Expected Behavior:

The intelligentOrder function should correctly sort the items based on the provided orderingFunction, handling potential errors within the ordering function and maintaining immutability.

Edge Cases to Consider:

  • Empty input array: Should return an empty array.
  • orderingFunction returning non-numeric values: Should be handled gracefully (treat as Infinity).
  • orderingFunction throwing errors: Should be handled gracefully (treat as Infinity).
  • Items with equal priority: The relative order of items with the same priority should be preserved (stable sort).

Examples

Example 1:

Input: [{name: "Task A", priority: 3}, {name: "Task B", priority: 1}, {name: "Task C", priority: 2}]
orderingFunction: (item) => item.priority
Output: [{name: "Task B", priority: 1}, {name: "Task C", priority: 2}, {name: "Task A", priority: 3}]
Explanation: Items are sorted based on the 'priority' property in ascending order.

Example 2:

Input: [10, 5, 20, 1]
orderingFunction: (item) => item
Output: [1, 5, 10, 20]
Explanation: Items are sorted numerically in ascending order.

Example 3: (Error Handling)

Input: [{name: "Task A"}, {name: "Task B", priority: 1}, {name: "Task C"}]
orderingFunction: (item) => item.priority || Infinity
Output: [{name: "Task B", priority: 1}, {name: "Task A"}, {name: "Task C"}]
Explanation: Task A and Task C do not have a 'priority' property, so the ordering function returns `Infinity` for them, placing them at the end.

Constraints

  • The items array can contain up to 1000 elements.
  • The orderingFunction should execute quickly (average execution time less than 1ms per item).
  • The input items array will always be an array.
  • The orderingFunction will always be a function.

Notes

  • Consider using the built-in sort method of arrays, but be mindful of the immutability requirement. You'll need to create a copy of the array before sorting.
  • The orderingFunction is a key part of this challenge. Think about how to handle potential errors within this function without crashing the entire sorting process.
  • A stable sort is preferred to preserve the original order of items with equal priority. JavaScript's native sort is not guaranteed to be stable across all browsers, so you might need to implement a stable sorting algorithm if strict stability is required. However, for the purpose of this challenge, preserving the original order is sufficient.
  • Focus on writing clean, readable, and well-documented code. Good error handling is crucial.
Loading editor...
typescript