From 089d4b5cee3cf688976ce0483758f6d83d457ba1 Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 7 Jun 2022 20:22:23 -0500 Subject: [PATCH] Update:Remove fast-sort dependency --- package-lock.json | 11 -- package.json | 1 - server/controllers/AuthorController.js | 2 +- server/controllers/LibraryController.js | 2 +- server/libs/fastSort/index.js | 131 ++++++++++++++++++++++++ server/objects/mediaTypes/Podcast.js | 2 +- server/utils/libraryHelpers.js | 2 +- 7 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 server/libs/fastSort/index.js diff --git a/package-lock.json b/package-lock.json index b5104019..c7d73f4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,6 @@ "express": "^4.17.1", "express-fileupload": "^1.2.1", "express-rate-limit": "^5.3.0", - "fast-sort": "^3.1.1", "fluent-ffmpeg": "^2.1.2", "fs-extra": "^10.0.0", "htmlparser2": "^8.0.1", @@ -795,11 +794,6 @@ "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-5.5.1.tgz", "integrity": "sha512-MTjE2eIbHv5DyfuFz4zLYWxpqVhEhkTiwFGuB74Q9CSou2WHO52nlE5y3Zlg6SIsiYUIPj6ifFxnkPz6O3sIUg==" }, - "node_modules/fast-sort": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/fast-sort/-/fast-sort-3.2.0.tgz", - "integrity": "sha512-EgQtkmWo2Icq6uei57fTrZAKayL9b4OISU1613737AuLcIbAZ57tcOtGaK2A7zO54kk97wOnSw6INDA++rjMAQ==" - }, "node_modules/finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -2535,11 +2529,6 @@ "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-5.5.1.tgz", "integrity": "sha512-MTjE2eIbHv5DyfuFz4zLYWxpqVhEhkTiwFGuB74Q9CSou2WHO52nlE5y3Zlg6SIsiYUIPj6ifFxnkPz6O3sIUg==" }, - "fast-sort": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/fast-sort/-/fast-sort-3.2.0.tgz", - "integrity": "sha512-EgQtkmWo2Icq6uei57fTrZAKayL9b4OISU1613737AuLcIbAZ57tcOtGaK2A7zO54kk97wOnSw6INDA++rjMAQ==" - }, "finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", diff --git a/package.json b/package.json index b19bcd3d..408e6174 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "express": "^4.17.1", "express-fileupload": "^1.2.1", "express-rate-limit": "^5.3.0", - "fast-sort": "^3.1.1", "fluent-ffmpeg": "^2.1.2", "fs-extra": "^10.0.0", "htmlparser2": "^8.0.1", diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index 856f8822..71ca67cf 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -1,6 +1,6 @@ const Logger = require('../Logger') const { reqSupportsWebp } = require('../utils/index') -const { createNewSortInstance } = require('fast-sort') +const { createNewSortInstance } = require('../libs/fastSort') const naturalSort = createNewSortInstance({ comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 281e6e31..5d61d308 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -4,7 +4,7 @@ const filePerms = require('../utils/filePerms') const Logger = require('../Logger') const Library = require('../objects/Library') const libraryHelpers = require('../utils/libraryHelpers') -const { sort, createNewSortInstance } = require('fast-sort') +const { sort, createNewSortInstance } = require('../libs/fastSort') const naturalSort = createNewSortInstance({ comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare }) diff --git a/server/libs/fastSort/index.js b/server/libs/fastSort/index.js new file mode 100644 index 00000000..1e3ad154 --- /dev/null +++ b/server/libs/fastSort/index.js @@ -0,0 +1,131 @@ +// SOURCE: https://github.com/snovakovic/fast-sort +// LICENSE: https://github.com/snovakovic/fast-sort/blob/master/LICENSE + +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['fast-sort'] = {})); +}(this, (function (exports) { + 'use strict'; + + // >>> INTERFACES <<< + // >>> HELPERS <<< + var castComparer = function (comparer) { return function (a, b, order) { return comparer(a, b, order) * order; }; }; + var throwInvalidConfigErrorIfTrue = function (condition, context) { + if (condition) + throw Error("Invalid sort config: " + context); + }; + var unpackObjectSorter = function (sortByObj) { + var _a = sortByObj || {}, asc = _a.asc, desc = _a.desc; + var order = asc ? 1 : -1; + var sortBy = (asc || desc); + // Validate object config + throwInvalidConfigErrorIfTrue(!sortBy, 'Expected `asc` or `desc` property'); + throwInvalidConfigErrorIfTrue(asc && desc, 'Ambiguous object with `asc` and `desc` config properties'); + var comparer = sortByObj.comparer && castComparer(sortByObj.comparer); + return { order: order, sortBy: sortBy, comparer: comparer }; + }; + // >>> SORTERS <<< + var multiPropertySorterProvider = function (defaultComparer) { + return function multiPropertySorter(sortBy, sortByArr, depth, order, comparer, a, b) { + var valA; + var valB; + if (typeof sortBy === 'string') { + valA = a[sortBy]; + valB = b[sortBy]; + } + else if (typeof sortBy === 'function') { + valA = sortBy(a); + valB = sortBy(b); + } + else { + var objectSorterConfig = unpackObjectSorter(sortBy); + return multiPropertySorter(objectSorterConfig.sortBy, sortByArr, depth, objectSorterConfig.order, objectSorterConfig.comparer || defaultComparer, a, b); + } + var equality = comparer(valA, valB, order); + if ((equality === 0 || (valA == null && valB == null)) && + sortByArr.length > depth) { + return multiPropertySorter(sortByArr[depth], sortByArr, depth + 1, order, comparer, a, b); + } + return equality; + }; + }; + function getSortStrategy(sortBy, comparer, order) { + // Flat array sorter + if (sortBy === undefined || sortBy === true) { + return function (a, b) { return comparer(a, b, order); }; + } + // Sort list of objects by single object key + if (typeof sortBy === 'string') { + throwInvalidConfigErrorIfTrue(sortBy.includes('.'), 'String syntax not allowed for nested properties.'); + return function (a, b) { return comparer(a[sortBy], b[sortBy], order); }; + } + // Sort list of objects by single function sorter + if (typeof sortBy === 'function') { + return function (a, b) { return comparer(sortBy(a), sortBy(b), order); }; + } + // Sort by multiple properties + if (Array.isArray(sortBy)) { + var multiPropSorter_1 = multiPropertySorterProvider(comparer); + return function (a, b) { return multiPropSorter_1(sortBy[0], sortBy, 1, order, comparer, a, b); }; + } + // Unpack object config to get actual sorter strategy + var objectSorterConfig = unpackObjectSorter(sortBy); + return getSortStrategy(objectSorterConfig.sortBy, objectSorterConfig.comparer || comparer, objectSorterConfig.order); + } + var sortArray = function (order, ctx, sortBy, comparer) { + var _a; + if (!Array.isArray(ctx)) { + return ctx; + } + // Unwrap sortBy if array with only 1 value to get faster sort strategy + if (Array.isArray(sortBy) && sortBy.length < 2) { + _a = sortBy, sortBy = _a[0]; + } + return ctx.sort(getSortStrategy(sortBy, comparer, order)); + }; + function createNewSortInstance(opts) { + var comparer = castComparer(opts.comparer); + return function (_ctx) { + var ctx = Array.isArray(_ctx) && !opts.inPlaceSorting + ? _ctx.slice() + : _ctx; + return { + asc: function (sortBy) { + return sortArray(1, ctx, sortBy, comparer); + }, + desc: function (sortBy) { + return sortArray(-1, ctx, sortBy, comparer); + }, + by: function (sortBy) { + return sortArray(1, ctx, sortBy, comparer); + }, + }; + }; + } + var defaultComparer = function (a, b, order) { + if (a == null) + return order; + if (b == null) + return -order; + if (a < b) + return -1; + if (a > b) + return 1; + return 0; + }; + var sort = createNewSortInstance({ + comparer: defaultComparer, + }); + var inPlaceSort = createNewSortInstance({ + comparer: defaultComparer, + inPlaceSorting: true, + }); + + exports.createNewSortInstance = createNewSortInstance; + exports.inPlaceSort = inPlaceSort; + exports.sort = sort; + + Object.defineProperty(exports, '__esModule', { value: true }); + +}))); diff --git a/server/objects/mediaTypes/Podcast.js b/server/objects/mediaTypes/Podcast.js index 2a3fd0c6..0dd3cafb 100644 --- a/server/objects/mediaTypes/Podcast.js +++ b/server/objects/mediaTypes/Podcast.js @@ -4,7 +4,7 @@ const PodcastMetadata = require('../metadata/PodcastMetadata') const { areEquivalent, copyValue } = require('../../utils/index') const abmetadataGenerator = require('../../utils/abmetadataGenerator') const { readTextFile } = require('../../utils/fileUtils') -const { createNewSortInstance } = require('fast-sort') +const { createNewSortInstance } = require('../../libs/fastSort') const naturalSort = createNewSortInstance({ comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare }) diff --git a/server/utils/libraryHelpers.js b/server/utils/libraryHelpers.js index b1ae59b4..316e2534 100644 --- a/server/utils/libraryHelpers.js +++ b/server/utils/libraryHelpers.js @@ -1,4 +1,4 @@ -const { sort, createNewSortInstance } = require('fast-sort') +const { sort, createNewSortInstance } = require('../libs/fastSort') const Logger = require('../Logger') const naturalSort = createNewSortInstance({ comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare