CJS -> ESM

This commit is contained in:
2025-03-05 12:19:35 -07:00
parent 5a8611afbc
commit bcc39d5f38
33 changed files with 228 additions and 282 deletions

View File

@@ -1,7 +1,3 @@
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const randomBool = (chance_of_failure = 0.25) => Math.random() < chance_of_failure;
module.exports = {
sleep, randomBool
};
export const randomBool = (chance_of_failure = 0.25) => Math.random() < chance_of_failure;

View File

@@ -3,7 +3,7 @@
* @param {number} length The length of the string
* @return {string} The generated string
*/
module.exports = (length) => {
export default (length) => {
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let text = "";

View File

@@ -1,6 +1,7 @@
const logger = require("./logger")(module);
import curriedLogger from "./logger.js";
const logger = curriedLogger(import.meta);
const typedefs = require("../typedefs");
import * as typedefs from "../typedefs.js";
/**
* Directed graph, may or may not be connected.
@@ -21,7 +22,7 @@ const typedefs = require("../typedefs");
* console.log(g.detectCycle()); // true
* ```
*/
class myGraph {
export class myGraph {
/**
* @param {string[]} nodes Graph nodes IDs
* @param {{ from: string, to: string }[]} edges Graph edges b/w nodes
@@ -156,4 +157,4 @@ class myGraph {
}
}
module.exports = myGraph;
export default myGraph;

View File

@@ -5,7 +5,7 @@
* @param {string} delimiter Delimiter of final string
* @returns {string}
*/
const getNestedValuesString = (obj, delimiter = ", ") => {
export const getNestedValuesString = (obj, delimiter = ", ") => {
let values = [];
for (key in obj) {
if (typeof obj[key] !== "object") {
@@ -17,7 +17,3 @@ const getNestedValuesString = (obj, delimiter = ", ") => {
return values.join(delimiter);
}
module.exports = {
getNestedValuesString
}

View File

@@ -1,10 +1,10 @@
const path = require("path");
import path from "path";
import { createLogger, transports, config, format } from "winston";
import * as typedefs from "../typedefs.js";
const { createLogger, transports, config, format } = require("winston");
const { combine, label, timestamp, printf, errors } = format;
const typedefs = require("../typedefs");
const getLabel = (callingModule) => {
if (!callingModule.filename) return "repl";
const parts = callingModule.filename?.split(path.sep);
@@ -35,9 +35,9 @@ const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
/**
* Creates a curried function, and call it with the module in use to get logs with filename
* @param {typedefs.Module} callingModule The module from which the logger is called
* @param {typedefs.Module} callingModule The module from which the logger is called (ESM - import.meta)
*/
const curriedLogger = (callingModule) => {
export const curriedLogger = (callingModule) => {
let winstonLogger = createLogger({
levels: config.npm.levels,
format: combine(
@@ -49,12 +49,12 @@ const curriedLogger = (callingModule) => {
transports: [
new transports.Console({ level: "info" }),
new transports.File({
filename: __dirname + "/../logs/debug.log",
filename: import.meta.dirname + "/../logs/debug.log",
level: "debug",
maxsize: 10485760,
}),
new transports.File({
filename: __dirname + "/../logs/error.log",
filename: import.meta.dirname + "/../logs/error.log",
level: "error",
maxsize: 1048576,
}),
@@ -64,4 +64,4 @@ const curriedLogger = (callingModule) => {
return winstonLogger;
}
module.exports = curriedLogger;
export default curriedLogger;

View File

@@ -1,4 +1,4 @@
const typedefs = require("../typedefs");
import * as typedefs from "../typedefs.js";
/** @type {RegExp} */
const base62Pattern = /^[A-Za-z0-9]+$/;
@@ -10,7 +10,7 @@ const base62Pattern = /^[A-Za-z0-9]+$/;
* @returns {typedefs.URIObject}
* @throws {TypeError} If the input is not a valid Spotify URI
*/
const parseSpotifyURI = (uri) => {
export const parseSpotifyURI = (uri) => {
const parts = uri.split(":");
if (parts[0] !== "spotify") {
@@ -59,7 +59,7 @@ const parseSpotifyURI = (uri) => {
* @returns {typedefs.URIObject}
* @throws {TypeError} If the input is not a valid Spotify link
*/
const parseSpotifyLink = (link) => {
export const parseSpotifyLink = (link) => {
const localPattern = /^https:\/\/open\.spotify\.com\/local\/([^\/]*)\/([^\/]*)\/([^\/]+)\/(\d+)$/;
const standardPattern = /^https:\/\/open\.spotify\.com\/([^\/]+)\/([^\/?]+)/;
@@ -106,7 +106,7 @@ const parseSpotifyLink = (link) => {
* @param {typedefs.URIObject} uriObj
* @returns {string}
*/
const buildSpotifyURI = (uriObj) => {
export const buildSpotifyURI = (uriObj) => {
if (uriObj.is_local) {
const artist = encodeURIComponent(uriObj.artist ?? "");
const album = encodeURIComponent(uriObj.album ?? "");
@@ -122,7 +122,7 @@ const buildSpotifyURI = (uriObj) => {
* @param {typedefs.URIObject} uriObj
* @returns {string}
*/
const buildSpotifyLink = (uriObj) => {
export const buildSpotifyLink = (uriObj) => {
if (uriObj.is_local) {
const artist = encodeURIComponent(uriObj.artist ?? "");
const album = encodeURIComponent(uriObj.album ?? "");
@@ -132,10 +132,3 @@ const buildSpotifyLink = (uriObj) => {
}
return `https://open.spotify.com/${uriObj.type}/${uriObj.id}`
}
module.exports = {
parseSpotifyURI,
parseSpotifyLink,
buildSpotifyURI,
buildSpotifyLink
}