diff --git a/filterApis.js b/filterApis.js new file mode 100644 index 0000000..084ad66 --- /dev/null +++ b/filterApis.js @@ -0,0 +1,64 @@ +import * as path from "path"; + +/** + * This is a pretty specific function. + * Given a list of filenames in a ./a/folder/filename-v23 format that may contain many filenames with different versions, + * it will return a list of filenames with only the highest version of each filename. + * For example, given + * + * ["../adyen-postman/postman/BinLookupService-v39.json", + * "../adyen-postman/postman/BinLookupService-v40.json", + * "../adyen-postman/postman/BinLookupService-v42.json", + * ["../adyen-postman/postman/CheckoutService-v70.json"] + * + * , the function will return + * + * ["../adyen-postman/postman/BinLookupService-v42.json", + * ["../adyen-postman/postman/CheckoutService-v70.json"] + * + * @constructor + * @param {Array.} filenames a list of filenames, including path and extension + * @param {Array.} filenames with only the highest version of each filename + */ +export function filenamesToSet(filenames){ + + const clearerApis = filenames.map((filename) => { + return { + "path" :filename, + "name" : path.parse(filename).name, + "root" : path.parse(filename).name.split("-")[0], + "version" : path.parse(filename).name.split("-v")[1], + } + }); + + const groupsOfApis = _groupBy(clearerApis, "root"); + const highestVersions = Object.values(groupsOfApis).map((value) => { + return value.reduce( + (prev, current) => { + return prev.version > current.version ? prev : current + }); + }); + + return highestVersions.map((value) => { return value.path});; +} + + +/** + * Group an array of objects using the given key + * @param {Array} xs an array of objects + * @param {String} key the key to group by + * @returns {Object} an object with each key being the value of the given key used to group by, and the value being an array of objects with that key + * @private + * + * @example + * // returns {"3": ["one", "two"], "5": ["three"]} + * groupBy(['one', 'two', 'three'], 'length') + * + * Thank you https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects! + */ +function _groupBy(xs, key) { + return xs.reduce(function(rv, x) { + (rv[x[key]] = rv[x[key]] || []).push(x); + return rv; + }, {}); +} \ No newline at end of file diff --git a/sort.js b/sort.js deleted file mode 100644 index 075d356..0000000 --- a/sort.js +++ /dev/null @@ -1,3 +0,0 @@ -export function list(){ - return []; -} \ No newline at end of file diff --git a/tests/filterApis.js b/tests/filterApis.js new file mode 100644 index 0000000..316ee4c --- /dev/null +++ b/tests/filterApis.js @@ -0,0 +1,36 @@ +import { test } from 'uvu'; +import * as assert from 'uvu/assert'; + +import {filenamesToSet} from '../filterApis.js'; + +const FULL_LIST = "../adyen-postman/postman/BalanceControlService-v1.json ../adyen-postman/postman/BalancePlatformService-v1.json ../adyen-postman/postman/BalancePlatformService-v2.json ../adyen-postman/postman/BinLookupService-v40.json ../adyen-postman/postman/BinLookupService-v50.json ../adyen-postman/postman/BinLookupService-v52.json ../adyen-postman/postman/BinLookupService-v53.json ../adyen-postman/postman/BinLookupService-v54.json ../adyen-postman/postman/CheckoutService-v37.json ../adyen-postman/postman/CheckoutService-v40.json ../adyen-postman/postman/CheckoutService-v41.json ../adyen-postman/postman/CheckoutService-v46.json ../adyen-postman/postman/CheckoutService-v49.json ../adyen-postman/postman/CheckoutService-v50.json ../adyen-postman/postman/CheckoutService-v51.json ../adyen-postman/postman/CheckoutService-v52.json ../adyen-postman/postman/CheckoutService-v53.json ../adyen-postman/postman/CheckoutService-v64.json ../adyen-postman/postman/CheckoutService-v65.json ../adyen-postman/postman/CheckoutService-v66.json ../adyen-postman/postman/CheckoutService-v67.json ../adyen-postman/postman/CheckoutService-v68.json ../adyen-postman/postman/CheckoutService-v69.json ../adyen-postman/postman/CheckoutService-v70.json ../adyen-postman/postman/DataProtectionService-v1.json ../adyen-postman/postman/LegalEntityService-v1.json ../adyen-postman/postman/LegalEntityService-v2.json ../adyen-postman/postman/LegalEntityService-v3.json ../adyen-postman/postman/ManagementService-v1.json ../adyen-postman/postman/PayoutService-v30.json ../adyen-postman/postman/PayoutService-v40.json ../adyen-postman/postman/PayoutService-v50.json ../adyen-postman/postman/PayoutService-v51.json ../adyen-postman/postman/PayoutService-v52.json ../adyen-postman/postman/PayoutService-v64.json ../adyen-postman/postman/PayoutService-v67.json ../adyen-postman/postman/PayoutService-v68.json ../adyen-postman/postman/RecurringService-v25.json ../adyen-postman/postman/RecurringService-v30.json ../adyen-postman/postman/RecurringService-v40.json ../adyen-postman/postman/RecurringService-v49.json ../adyen-postman/postman/RecurringService-v67.json ../adyen-postman/postman/RecurringService-v68.json ../adyen-postman/postman/StoredValueService-v46.json ../adyen-postman/postman/TestCardService-v1.json ../adyen-postman/postman/TfmAPIService-v1.json ../adyen-postman/postman/TransferService-v1.json ../adyen-postman/postman/TransferService-v2.json ../adyen-postman/postman/TransferService-v3.json" +const FULL_FILENAMES = FULL_LIST.split(" "); + +const CLEAN_LIST = "../adyen-postman/postman/BalanceControlService-v1.json ../adyen-postman/postman/BalancePlatformService-v2.json ../adyen-postman/postman/BinLookupService-v54.json ../adyen-postman/postman/CheckoutService-v70.json ../adyen-postman/postman/DataProtectionService-v1.json ../adyen-postman/postman/LegalEntityService-v3.json ../adyen-postman/postman/ManagementService-v1.json ../adyen-postman/postman/PayoutService-v68.json ../adyen-postman/postman/RecurringService-v68.json ../adyen-postman/postman/StoredValueService-v46.json ../adyen-postman/postman/TestCardService-v1.json ../adyen-postman/postman/TfmAPIService-v1.json ../adyen-postman/postman/TransferService-v3.json" +const CLEAN_FILENAMES = CLEAN_LIST.split(" "); + +test('filenamesToSet empty', () => { + assert.equal(filenamesToSet([]), []); +}); + +test('filenamesToSet no reduction', () => { + assert.equal(filenamesToSet( + ["../adyen-postman/postman/BalanceControlService-v1.json", "../adyen-postman/postman/BinLookupService-v40.json"] + ), + ["../adyen-postman/postman/BalanceControlService-v1.json", "../adyen-postman/postman/BinLookupService-v40.json"]); +}); + +test('filenamesToSet simple, reduction', () => { + assert.equal(filenamesToSet( + ["../adyen-postman/postman/BinLookupService-v39.json", "../adyen-postman/postman/BinLookupService-v40.json", "../adyen-postman/postman/BinLookupService-v42.json"] + ), + ["../adyen-postman/postman/BinLookupService-v42.json"]); +}); + +test('filenamesToSet full', () => { + assert.equal(filenamesToSet(FULL_FILENAMES), CLEAN_FILENAMES); +}); + +test.run(); + + diff --git a/tests/sort.js b/tests/sort.js deleted file mode 100644 index 946f136..0000000 --- a/tests/sort.js +++ /dev/null @@ -1,10 +0,0 @@ -import { test } from 'uvu'; -import * as assert from 'uvu/assert'; - -import {list} from '../sort.js'; -test('sort.list', () => { - assert.equal(list(), []); -}); - - -test.run(); \ No newline at end of file