From 55abf2f48073408262dc06f3bfe42f3979b47e18 Mon Sep 17 00:00:00 2001 From: Konrad Guzior <36774785+MrGuzior@users.noreply.github.com> Date: Wed, 4 Oct 2023 14:32:42 +0200 Subject: [PATCH] Feature/add user id and json (#92) * Add json and userId helpers * Bump version * Export functions * abcdefghijklmnopqrstuvwxyz --- index.js | 6 ++++++ lib/utils/json.js | 10 ++++++++++ lib/utils/userId.js | 26 ++++++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- test/feature/index-test.js | 4 ++++ test/unit/utils/userId-test.js | 20 ++++++++++++++++++++ 7 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 lib/utils/json.js create mode 100644 lib/utils/userId.js create mode 100644 test/unit/utils/userId-test.js diff --git a/index.js b/index.js index c032f05..2d29da4 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,8 @@ const ses = require("./lib/utils/ses"); const sftp = require("./lib/utils/sftp"); const streams = require("./lib/utils/streams"); const swedishBankday = require("./lib/utils/swedish-bankday"); +const json = require("./lib/utils/json"); +const userId = require("./lib/utils/userId"); // validation helpers const countryCodes = require("./lib/validation-helpers/country-codes"); @@ -55,6 +57,9 @@ module.exports = { gcs, http, iterators, + json, + parseUserId: userId.parseUserId, + parseUserIdParts: userId.parseUserIdParts, PDF, pdfGenerator, s3, @@ -62,6 +67,7 @@ module.exports = { sftp, streams, swedishBankday, + userId, // validation helpers countryCodes, formattingHelpers, diff --git a/lib/utils/json.js b/lib/utils/json.js new file mode 100644 index 0000000..277bba7 --- /dev/null +++ b/lib/utils/json.js @@ -0,0 +1,10 @@ +"use strict"; +const fs = require("fs"); + +function importJson(path) { + const root = process.cwd(); + + return JSON.parse(fs.readFileSync(`${root}/${path}`).toString()); +} + +module.exports = { importJson }; diff --git a/lib/utils/userId.js b/lib/utils/userId.js new file mode 100644 index 0000000..310e8e2 --- /dev/null +++ b/lib/utils/userId.js @@ -0,0 +1,26 @@ +"use strict"; + +function parseUserId(namespacedUserId) { + const [ namespace, userId ] = namespacedUserId.split("://"); + if (userId.startsWith("splus/")) { + return { namespace, userId: userId.substr(6, userId.length - 1) }; + } else { + return { namespace, userId }; + } +} + +function parseUserIdParts(namespacedUserId) { + const [ namespace, fullUserId ] = namespacedUserId.split("://"); + if (!fullUserId) return {}; + const [ origin, userId ] = fullUserId.split("/"); + if (!userId) { + return { namespace, origin: undefined, userId: origin }; + } + + return { namespace, origin, userId }; +} + +module.exports = { + parseUserId, + parseUserIdParts, +}; diff --git a/package-lock.json b/package-lock.json index 3c3c158..f0065c9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "lu-common", - "version": "3.9.1", + "version": "3.9.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "lu-common", - "version": "3.9.1", + "version": "3.9.2", "license": "UNLICENSED", "dependencies": { "@google-cloud/storage": "^5.18.3", diff --git a/package.json b/package.json index 5090632..599b183 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lu-common", - "version": "3.9.1", + "version": "3.9.2", "description": "", "main": "index.js", "engines": { diff --git a/test/feature/index-test.js b/test/feature/index-test.js index 1de6245..84612f2 100644 --- a/test/feature/index-test.js +++ b/test/feature/index-test.js @@ -22,6 +22,9 @@ const expectedExports = [ "gcs", "http", "iterators", + "json", + "parseUserIdParts", + "parseUserId", "PDF", "pdfGenerator", "s3", @@ -29,6 +32,7 @@ const expectedExports = [ "sftp", "streams", "swedishBankday", + "userId", // validation helpers "countryCodes", "formattingHelpers", diff --git a/test/unit/utils/userId-test.js b/test/unit/utils/userId-test.js new file mode 100644 index 0000000..2a3791b --- /dev/null +++ b/test/unit/utils/userId-test.js @@ -0,0 +1,20 @@ +"use strict"; + +const expect = require("chai").expect; + +const { parseUserId, parseUserIdParts } = require("../../../lib/utils/userId"); + +describe("parseUserId", () => { + it("should parse userId", () => { + expect(parseUserId("expressen://db8a734a-b1aa-4648-8cdd-67825906673e")).to.eql({ namespace: "expressen", userId: "db8a734a-b1aa-4648-8cdd-67825906673e" }); + expect(parseUserId("di://splus/0gJZ1gPX7lj8uPAA8PlFjX")).to.eql({ namespace: "di", userId: "0gJZ1gPX7lj8uPAA8PlFjX" }); + }); +}); + +describe("parseUserIdParts", () => { + it("should parse userId", () => { + expect(parseUserIdParts("expressen://")).to.eql({}); + expect(parseUserIdParts("expressen://db8a734a-b1aa-4648-8cdd-67825906673e")).to.eql({ namespace: "expressen", origin: undefined, userId: "db8a734a-b1aa-4648-8cdd-67825906673e" }); + expect(parseUserIdParts("di://splus/0gJZ1gPX7lj8uPAA8PlFjX")).to.eql({ namespace: "di", origin: "splus", userId: "0gJZ1gPX7lj8uPAA8PlFjX" }); + }); +});