UNPKG

quantitivecalc

Version:

A TypeScript library providing advanced quantitative finance functions for risk analysis, performance metrics, and technical indicators. (Currently in development)

168 lines (167 loc) 8.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const sortByColumn_1 = __importDefault(require("./sortByColumn")); describe('sortByColumn', () => { // Test data const people = [ { name: 'Alice', age: 30, email: 'alice@test.com' }, { name: 'Bob', age: 25, email: 'bob@test.com' }, { name: 'Charlie', age: 35, email: undefined }, { name: 'Diana', age: 28, email: 'diana@test.com' }, { name: 'Eve', age: 32, email: null } ]; const products = [ { id: 3, name: 'Laptop', price: 999.99, category: 'Electronics', inStock: true }, { id: 1, name: 'Book', price: 19.99, category: 'Education', inStock: false }, { id: 2, name: 'Phone', price: 599.99, category: 'Electronics', inStock: true }, { id: 4, name: 'Desk', price: 299.99, category: 'Furniture', inStock: true } ]; describe('String sorting', () => { test('should sort by name ascending', () => { const result = (0, sortByColumn_1.default)(people, 'name', true); expect(result.map(p => p.name)).toEqual(['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']); }); test('should sort by name descending', () => { const result = (0, sortByColumn_1.default)(people, 'name', false); expect(result.map(p => p.name)).toEqual(['Eve', 'Diana', 'Charlie', 'Bob', 'Alice']); }); test('should sort by category ascending', () => { const result = (0, sortByColumn_1.default)(products, 'category', true); expect(result.map(p => p.category)).toEqual(['Education', 'Electronics', 'Electronics', 'Furniture']); }); }); describe('Number sorting', () => { test('should sort by age ascending', () => { const result = (0, sortByColumn_1.default)(people, 'age', true); expect(result.map(p => p.age)).toEqual([25, 28, 30, 32, 35]); }); test('should sort by age descending', () => { const result = (0, sortByColumn_1.default)(people, 'age', false); expect(result.map(p => p.age)).toEqual([35, 32, 30, 28, 25]); }); test('should sort by price ascending', () => { const result = (0, sortByColumn_1.default)(products, 'price', true); expect(result.map(p => p.price)).toEqual([19.99, 299.99, 599.99, 999.99]); }); test('should sort by id descending', () => { const result = (0, sortByColumn_1.default)(products, 'id', false); expect(result.map(p => p.id)).toEqual([4, 3, 2, 1]); }); }); describe('Boolean sorting', () => { test('should sort by boolean ascending (false first)', () => { const result = (0, sortByColumn_1.default)(products, 'inStock', true); expect(result.map(p => p.inStock)).toEqual([false, true, true, true]); }); test('should sort by boolean descending (true first)', () => { const result = (0, sortByColumn_1.default)(products, 'inStock', false); expect(result.map(p => p.inStock)).toEqual([true, true, true, false]); }); }); describe('Null/undefined handling', () => { test('should handle null/undefined values ascending (nulls first)', () => { const result = (0, sortByColumn_1.default)(people, 'email', true); const emails = result.map(p => p.email); // First two should be null/undefined expect(emails.slice(0, 2).every(email => email == null)).toBe(true); // Remaining should be sorted strings expect(emails.slice(2)).toEqual(['alice@test.com', 'bob@test.com', 'diana@test.com']); }); test('should handle null/undefined values descending (nulls last)', () => { const result = (0, sortByColumn_1.default)(people, 'email', false); const emails = result.map(p => p.email); // First three should be sorted strings (descending) expect(emails.slice(0, 3)).toEqual(['diana@test.com', 'bob@test.com', 'alice@test.com']); // Last two should be null/undefined expect(emails.slice(3).every(email => email == null)).toBe(true); }); }); describe('Edge cases', () => { test('should handle empty array', () => { const result = (0, sortByColumn_1.default)([], 'name', true); expect(result).toEqual([]); }); test('should handle single item array', () => { const singlePerson = [{ name: 'Solo', age: 25, email: 'solo@test.com' }]; const result = (0, sortByColumn_1.default)(singlePerson, 'name', true); expect(result).toEqual(singlePerson); expect(result).not.toBe(singlePerson); // Should be a new array }); test('should not mutate original array', () => { const original = [...people]; const result = (0, sortByColumn_1.default)(people, 'age', true); expect(people).toEqual(original); // Original unchanged expect(result).not.toBe(people); // Different array reference }); test('should handle identical values', () => { const duplicates = [ { name: 'Same', age: 25 }, { name: 'Same', age: 25 }, { name: 'Same', age: 25 } ]; const result = (0, sortByColumn_1.default)(duplicates, 'name', true); expect(result.length).toBe(3); expect(result.every(item => item.name === 'Same')).toBe(true); }); }); describe('Complex sorting scenarios', () => { test('should maintain stable sort for equal values', () => { const items = [ { id: 1, category: 'A', order: 1 }, { id: 2, category: 'A', order: 2 }, { id: 3, category: 'B', order: 3 }, { id: 4, category: 'A', order: 4 } ]; const result = (0, sortByColumn_1.default)(items, 'category', true); const categoryA = result.filter(item => item.category === 'A'); // Items with same category should maintain relative order expect(categoryA.map(item => item.id)).toEqual([1, 2, 4]); }); test('should work with mixed data types in union types', () => { const mixed = [ { value: 'banana', id: 1 }, { value: 100, id: 2 }, { value: 'apple', id: 3 }, { value: 50, id: 4 } ]; const result = (0, sortByColumn_1.default)(mixed, 'value', true); // In JS string comparison, numbers are converted to strings: "100" < "50" < "apple" < "banana" expect(result.map(item => item.value)).toEqual(['banana', 100, 'apple', 50]); }); }); describe('Type safety', () => { test('should work with different object types', () => { const items = [ { key: 'z', value: 1 }, { key: 'a', value: 3 }, { key: 'm', value: 2 } ]; const byKey = (0, sortByColumn_1.default)(items, 'key', true); const byValue = (0, sortByColumn_1.default)(items, 'value', false); expect(byKey.map(i => i.key)).toEqual(['a', 'm', 'z']); expect(byValue.map(i => i.value)).toEqual([3, 2, 1]); }); }); }); // Performance test (optional) describe('Performance', () => { test('should handle large datasets efficiently', () => { const largeDataset = Array.from({ length: 10000 }, (_, i) => ({ id: i, name: `Item ${Math.random().toString(36).substring(7)}`, value: Math.random() * 1000 })); const start = performance.now(); const result = (0, sortByColumn_1.default)(largeDataset, 'value', true); const end = performance.now(); expect(result.length).toBe(10000); expect(end - start).toBeLessThan(100); // Should complete in less than 100ms // Verify it's actually sorted for (let i = 1; i < result.length; i++) { expect(result[i].value).toBeGreaterThanOrEqual(result[i - 1].value); } }); });