Skip to content

Commit

Permalink
Feature/add user id and json (#92)
Browse files Browse the repository at this point in the history
* Add json and userId helpers

* Bump version

* Export functions

* abcdefghijklmnopqrstuvwxyz
  • Loading branch information
MrGuzior authored Oct 4, 2023
1 parent 28ca4c4 commit 55abf2f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -55,13 +57,17 @@ module.exports = {
gcs,
http,
iterators,
json,
parseUserId: userId.parseUserId,
parseUserIdParts: userId.parseUserIdParts,
PDF,
pdfGenerator,
s3,
ses,
sftp,
streams,
swedishBankday,
userId,
// validation helpers
countryCodes,
formattingHelpers,
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/json.js
Original file line number Diff line number Diff line change
@@ -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 };
26 changes: 26 additions & 0 deletions lib/utils/userId.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lu-common",
"version": "3.9.1",
"version": "3.9.2",
"description": "",
"main": "index.js",
"engines": {
Expand Down
4 changes: 4 additions & 0 deletions test/feature/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ const expectedExports = [
"gcs",
"http",
"iterators",
"json",
"parseUserIdParts",
"parseUserId",
"PDF",
"pdfGenerator",
"s3",
"ses",
"sftp",
"streams",
"swedishBankday",
"userId",
// validation helpers
"countryCodes",
"formattingHelpers",
Expand Down
20 changes: 20 additions & 0 deletions test/unit/utils/userId-test.js
Original file line number Diff line number Diff line change
@@ -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" });
});
});

0 comments on commit 55abf2f

Please sign in to comment.