Skip to content

Commit

Permalink
Feature/swedish bankday (#91)
Browse files Browse the repository at this point in the history
- Add swedishBankday
- make index-test more generic
- export toggle
- cleanup order of things
- bump version
- bump get-func-name
  • Loading branch information
varney authored Oct 3, 2023
1 parent 2ed8935 commit 28ca4c4
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"ui": "mocha-cakes-2",
"exit": true,
"require": [
"./test/helpers/setup.js"
"./test/setup.js"
]
}
3 changes: 3 additions & 0 deletions config/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
],
"logging": {
"pretty": true
},
"toggle": {
"pafInCommonNamespaces": true
}
}
65 changes: 40 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
"use strict";

const namespaces = require("./lib/namespaces");
const titles = require("./lib/titles");
const schemas = require("./lib/validation-helpers/schemas");
const countryCodes = require("./lib/validation-helpers/country-codes");
const formattingHelpers = require("./lib/validation-helpers/formatting-helpers");

const stripSchemaTag = require("./lib/validation-helpers/strip-joi-schema-tags");

// helpers
const caseBodyHelper = require("./lib/helpers/case-body-helper");
const codeHelper = require("./lib/helpers/code-helper");
const toggle = require("./lib/helpers/toggle");

// utils
const email = require("./lib/utils/email");
const ftp = require("./lib/utils/ftp");
const gcpAuth = require("./lib/utils/gcp-auth");
const gcs = require("./lib/utils/gcs");
const http = require("./lib/utils/http");
const iterators = require("./lib/utils/iterators");
const PDF = require("./lib/utils/pdf");
const pdfGenerator = require("./lib/utils/pdfGenerator");
const s3 = require("./lib/utils/s3");
const ses = require("./lib/utils/ses");
const sftp = require("./lib/utils/sftp");
const streams = require("./lib/utils/streams");
const http = require("./lib/utils/http");
const swedishBankday = require("./lib/utils/swedish-bankday");

// validation helpers
const countryCodes = require("./lib/validation-helpers/country-codes");
const formattingHelpers = require("./lib/validation-helpers/formatting-helpers");
const schemas = require("./lib/validation-helpers/schemas");
const stripSchemaTag = require("./lib/validation-helpers/strip-schema-tag");

// other
const namespaces = require("./lib/namespaces");
const titles = require("./lib/titles");

// test helpers
const clone = require("./test/helpers/clone");
const fakeApi = require("./test/helpers/fake-api");
const fakeFtp = require("./test/helpers/fake-ftp");
const fakeGcpAuth = require("./test/helpers/fake-gcp-auth");
Expand All @@ -33,38 +42,44 @@ const fakeSftp = require("./test/helpers/fake-sftp");
const fileUtils = require("./test/helpers/file-utils");
const messageHelper = require("./test/helpers/message-helper");
const pdfReader = require("./test/helpers/pdfReader");
const clone = require("./test/helpers/clone");

module.exports = {
// helpers
caseBodyHelper,
codeHelper,
countryCodes,
toggle,
// utils
email,
fakeApi,
fakeFtp,
fakeGcpAuth,
fakeGcs,
fakeS3,
fakeSes,
fakeSftp,
fileUtils,
formattingHelpers,
ftp,
gcpAuth,
gcs,
http,
iterators,
messageHelper,
namespaces,
titles,
PDF,
pdfGenerator,
pdfReader,
schemas,
s3,
ses,
sftp,
streams,
swedishBankday,
// validation helpers
countryCodes,
formattingHelpers,
schemas,
stripSchemaTag,
// other
namespaces,
titles,
// test helpers
clone,
fakeApi,
fakeFtp,
fakeGcpAuth,
fakeGcs,
fakeS3,
fakeSes,
fakeSftp,
fileUtils,
messageHelper,
pdfReader,
};
12 changes: 3 additions & 9 deletions lib/helpers/toggle.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
"use strict";
const config = require("exp-config");
const { toggle: configToggles = {} } = require("exp-config");

const knownToggles = [
"pafInCommonNamespaces",
];

knownToggles.sort();

config.toggle = config.toggle || {};
const knownToggles = Object.keys(configToggles).sort();

/* c8 ignore start */
function toggle(name) {
Expand All @@ -18,7 +12,7 @@ function toggle(name) {
if (process.env["NODE-DISABLE-TOGGLE"] === name) return false;
return true;
}
const value = config.toggle[name];
const value = configToggles[name];
return value === true || value === "true";
}
/* c8 ignore stop */
Expand Down
43 changes: 43 additions & 0 deletions lib/utils/swedish-bankday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
const moment = require("moment");
const { isHoliday } = require("swedish-holidays");
const bankHolidays = [
"Nyårsdagen",
"Trettondedag jul",
"Långfredagen",
"Påskafton",
"Påskdagen",
"Annandag påsk",
"Första maj",
"Kristi himmelsfärdsdag",
"Pingstafton",
"Pingstdagen",
"Sveriges nationaldag",
"Midsommarafton",
"Midsommardagen",
"Alla helgons dag",
"Julafton",
"Juldagen",
"Annandag jul",
"Nyårsafton",
];
function isBankDay(d) {
const md = moment(d);
// weekends are never bankdays
if ([ 0, 6 ].includes(md.weekday())) return false;
const holiday = isHoliday(md.toDate());
// not a holiday
if (!holiday) return true;

return !bankHolidays.includes(holiday.name);
}

function nextBankDay(d) {
let md = moment(d);
do {
md = md.add(1, "day");
} while (!isBankDay(md));
return md.format("YYYY-MM-DD");
}

module.exports = { isBankDay, nextBankDay };
File renamed without changes.
16 changes: 11 additions & 5 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lu-common",
"version": "3.9.0",
"version": "3.9.1",
"description": "",
"main": "index.js",
"engines": {
Expand Down Expand Up @@ -35,6 +35,7 @@
"pdfkit": "^0.13.0",
"sinon": "^15.0.0",
"ssh2-sftp-client": "^7.2.3",
"swedish-holidays": "^1.1.2",
"urlencode": "^1.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/validation-helpers/schemas-test-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const stripSchemaTag = require("../../../lib/validation-helpers/strip-joi-schema-tags");
const stripSchemaTag = require("../../../lib/validation-helpers/strip-schema-tag");

const { addressSchema } = require("../../../lib/validation-helpers/schemas");

Expand Down
85 changes: 67 additions & 18 deletions test/feature/index-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
"use strict";

const common = require("../../index");
const expect = require("chai").expect;
const fs = require("fs");
const path = require("path");

const paths = [ "lib", "test/helpers" ];
const allExports = [];
for (const basePath of paths) {
allExports.push(...getPathExports(basePath));
}

const expectedExports = [
// helpers
"caseBodyHelper",
"codeHelper",
"namespaces",
"titles",
"schemas",
"toggle",
// utils
"email",
"ftp",
"gcpAuth",
Expand All @@ -21,6 +28,17 @@ const expectedExports = [
"ses",
"sftp",
"streams",
"swedishBankday",
// validation helpers
"countryCodes",
"formattingHelpers",
"schemas",
"stripSchemaTag",
// other
"namespaces",
"titles",
// test helpers
"clone",
"fakeApi",
"fakeFtp",
"fakeGcpAuth",
Expand All @@ -31,25 +49,56 @@ const expectedExports = [
"fileUtils",
"messageHelper",
"pdfReader",
"stripSchemaTag",
"countryCodes",
"formattingHelpers",
"clone",
];

describe("Exposed features", () => {
const exports = [];

const exposedExports = [];
for (const c in common) {
exports.push(c.toString());
exposedExports.push(c.toString());
}

describe("Importing default export", () => {
it("The right stuff gets imported", () => {
const list = exports.filter((val) => !expectedExports.includes(val));
const list2 = expectedExports.filter((val) => !exports.includes(val));
expect(list.length).to.equal(0);
expect(list2.length).to.equal(0);
});
describe("everything we expect to export is exposed", () => {
for (const expectedExport of expectedExports) {
it(`${expectedExport} should be exposed`, () => {
exposedExports.should.include(expectedExport);
});
}
});

describe("everything we expose is expected", () => {
for (const exposedExport of exposedExports) {
it(`${exposedExport} is supposed to be exposed`, () => {
expectedExports.should.include(exposedExport);
});
}
});

describe("everything exported by modules is exposed in lu-common", () => {
for (const expectedExport of allExports) {
it(`should export ${expectedExport}`, () => {
exposedExports.should.include(expectedExport);
});
}
});
});

function toCamelCase(fileName) {
return fileName.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
}

function getPathExports(basePath) {
const exports = [];
const normalizedPath = path.join(__dirname, "..", "..", basePath);
fs.readdirSync(normalizedPath).forEach((file) => {
const filePath = path.join(normalizedPath, file);
const stats = fs.statSync(filePath);

// get all exports from subdirectories too
if (stats.isDirectory()) exports.push(...getPathExports(path.join(basePath, file)));
else {
const importName = file === "pdf.js" ? "PDF" : toCamelCase(file.replace(".js", ""));
exports.push(importName);
}
});
return exports;
}
File renamed without changes.
Loading

0 comments on commit 28ca4c4

Please sign in to comment.