Skip to content

Commit

Permalink
feat: remove moment dependency
Browse files Browse the repository at this point in the history
the moment dependency accounts for 59% of the bundle
size of snyk-to-html pacakge and only used for a
simple (and predefined?) formatting option.

source: https://bundlephobia.com/package/snyk-to-html@2.5.1
  • Loading branch information
lirantal committed Jul 17, 2024
1 parent c0469d0 commit 41b22ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"lodash.isempty": "^4.4.0",
"lodash.orderby": "^4.6.0",
"marked": "^4.0.12",
"moment": "^2.29.4",
"source-map-support": "^0.5.16",
"uglify-js": "^3.15.1"
},
Expand Down
50 changes: 50 additions & 0 deletions src/lib/dateutil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export function formatDateTime(date, format): string {
if (!date) {
date = new Date();
}
const day = date.getUTCDate();
const ordinalSuffix = getOrdinalSuffix(day);
const dayWithSuffix = `${day}${ordinalSuffix}`;

const hours = date.getUTCHours();
const minutes = date.getUTCMinutes();
const seconds = date.getUTCSeconds();
const ampm = hours >= 12 ? 'pm' : 'am';
const formattedHours = hours % 12 || 12;

const monthNames = [
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
];

const replacements = {
'MMMM': monthNames[date.getUTCMonth()],
'Do': dayWithSuffix,
'YYYY': date.getUTCFullYear(),
'h': formattedHours,
'mm': minutes.toString().padStart(2, '0'),
'ss': seconds.toString().padStart(2, '0'),
'a': ampm,
'z': 'UTC',
'Z': '+00:00'
};

return format.replace(/MMMM|Do|YYYY|h|mm|ss|a|z|Z/g, (match) => replacements[match]);
}

function getOrdinalSuffix(day) {
if (day > 3 && day < 21) {
return 'th';
}
switch (day % 10) {
case 1:
return 'st';
case 2:
return 'nd';
case 3:
return 'rd';
default:
return 'th';
}
}
10 changes: 6 additions & 4 deletions src/lib/snyk-to-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import * as debugModule from 'debug';
import fs = require('fs');
import Handlebars = require('handlebars');
import marked = require('marked');
import moment = require('moment');
import path = require('path');
import { addIssueDataToPatch, getUpgrades, severityMap, IacProjectType } from './vuln';
import {
processSourceCode,
} from './codeutil';
import {
formatDateTime
} from './dateutil';

const debug = debugModule('snyk-to-html');

Expand Down Expand Up @@ -99,7 +101,7 @@ class SnykToHtml {
export { SnykToHtml };

function metadataForVuln(vuln: any) {
let {cveSpaced, cveLineBreaks} = concatenateCVEs(vuln)
const {cveSpaced, cveLineBreaks} = concatenateCVEs(vuln)

return {
id: vuln.id,
Expand Down Expand Up @@ -127,7 +129,7 @@ function concatenateCVEs(vuln: any) {

if (vuln.identifiers) {
vuln.identifiers.CVE.forEach(function(c) {
let cveLink = `<a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=${c}">${c}</a>`
const cveLink = `<a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=${c}">${c}</a>`
cveSpaced += `${cveLink}&nbsp;`
cveLineBreaks += `${cveLink}</br>`
})
Expand Down Expand Up @@ -381,7 +383,7 @@ async function readInputFromStdin(): Promise<string> {
// handlebar helpers
const hh = {
markdown: marked.parse,
moment: (date, format) => moment.utc(date).format(format),
moment: (date, format) => formatDateTime(date, format),
count: data => data && data.length,
dump: (data, spacer) => JSON.stringify(data, null, spacer || null),
// block helpers
Expand Down

0 comments on commit 41b22ec

Please sign in to comment.