Skip to content

Commit a6a4d48

Browse files
adding cjs support
1 parent e4d3073 commit a6a4d48

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![npm](https://img.shields.io/npm/v/base-ex?color=%23009911&style=for-the-badge)](https://www.npmjs.com/package/base-ex)
55

66

7-
**BaseEx** is a collection of classes for data representation from Base16 (hex) to BasE91.
7+
**BaseEx** is a collection of classes for data representation from Base16 (hex) to basE91.
88
BaseEx is completely standalone and works client and server side.
99
There are other good solutions for e.g. Base32, Base64, Base85, but BaseEx has them all in one place.
1010
The **Ex** in the name stands for **Ex**ponent (of n) or - as read out loud - for an **X**.
@@ -30,7 +30,7 @@ git clone https://github.com/UmamiAppearance/BaseExJS.git
3030
nmp install base-ex
3131
```
3232

33-
## Build
33+
## Builds
3434
The GitHub repository has ready to use builds included. You can find them in [dist](https://github.com/UmamiAppearance/BaseExJS/tree/main/dist). The npm package comes without pre build files.
3535

3636
For building you have to run:
@@ -71,11 +71,16 @@ import {Base32} from "./path/BaseEx.esm.min.js"
7171

7272
#### Node
7373
```js
74+
// ESM6 Module
75+
7476
// main class
7577
import {BaseEx} from "base-ex"
7678

77-
// explicit converter (e.g. Base32)
78-
import {Base32} from "base-ex"
79+
// explicit converter (e.g. Base64)
80+
import {Base64} from "base-ex"
81+
82+
// CommonJS
83+
const BaseEx = require("base-ex");
7984
```
8085

8186
#### Available imports Browser/Node
@@ -86,6 +91,8 @@ The **classic import** via script tag has them all available without further ado
8691
* ...
8792
* ``BaseEx.BaseEx``
8893

94+
The same goes for the CommonJS import from Node. The only difference is, that the scope is not necessarily named ``BaseEx``, as this is defined by the user (``const myName = require("base-ex") --> myName.Base16...``).
95+
8996
Full **import** for **ES6** modules:
9097

9198
```js

demo.html

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
decode: Object.keys(baseEx),
4141
},
4242
importType: "esm",
43-
importTypes: ["esm", "node", "iife"],
43+
importTypes: ["esm", "node-esm", "node-cjs", "iife"],
4444
};
4545
// Set the initial output
4646
Data.output = baseEx[Data.curTypes.decode][Data.mode](Data.input, Data.curTypes.encode);
@@ -262,17 +262,21 @@
262262
const use = (isBytes) ? "setting as default:" : "making use of default";
263263

264264
// import types
265-
const importComment = (this.importType !== "iife") ? `${this.importType}-module` : this.importType;
265+
const importComment = (this.importType.match(".*esm")) ? `${this.importType}-module` : this.importType;
266266
let importStatement;
267-
let iife = "";
267+
let scopeVar = "";
268268
let note = "// Note:\n// - It is not necessary to import both modules\n// (use only what you need)";
269269
if (this.importType === "esm") {
270270
importStatement = `import { ${subFNName}, BaseEx } from "./path/BaseEx.esm.min.js";`;
271271
note += "\n// - remember to adjust the import path";
272-
} else if (this.importType === "node") {
272+
} else if (this.importType === "node-esm") {
273273
importStatement = `import { ${subFNName}, BaseEx } from "base-ex";`;
274+
} else if (this.importType === "node-cjs") {
275+
importStatement = `const BaseEx = require("base-ex");`;
276+
scopeVar = "BaseEx.";
277+
note = "";
274278
} else {
275-
iife = "BaseEx.";
279+
scopeVar = "BaseEx.";
276280
importStatement = `// use the following line as your script tag:\n// <script src="path/BaseEx.iise.min.js"></script>`;
277281
note = "// Note:\n// - remember to adjust the path in your script tag";
278282
}
@@ -288,19 +292,19 @@
288292
const input = ${input.replace("<", "&lt;").replace(">", "&gt;")};
289293
290294
// Main class BaseEx
291-
const baseExA = new ${iife}BaseEx();
295+
const baseExA = new ${scopeVar}BaseEx();
292296
const outputA = baseExA.${this.curTypes.decode}.${this.mode}(input, "${this.curTypes.encode}");
293297
294298
// Main class BaseEx, ${use} ${this.curTypes.encode}-type
295-
const baseExB = new ${iife}BaseEx(${fixedType.substr(2)});
299+
const baseExB = new ${scopeVar}BaseEx(${fixedType.substr(2)});
296300
const outputB = baseExB.${this.curTypes.decode}.${this.mode}(input);
297301
298302
// Direct use of the required base converter
299-
const ${this.curTypes.decode}A = new ${iife}${subFNName}(${version});
303+
const ${this.curTypes.decode}A = new ${scopeVar}${subFNName}(${version});
300304
const outputC = ${this.curTypes.decode}A.${this.mode}(input, "${this.curTypes.encode}");
301305
302306
// Direct use of the base converter, ${use} ${this.curTypes.encode}-type
303-
const ${this.curTypes.decode}B = new ${iife}${subFNName}("${baseEx[this.curTypes.decode].version}"${fixedType});
307+
const ${this.curTypes.decode}B = new ${scopeVar}${subFNName}("${baseEx[this.curTypes.decode].version}"${fixedType});
304308
const outputD = ${this.curTypes.decode}B.${this.mode}(input);
305309
306310
// ${copyPasteInfo}
@@ -393,7 +397,7 @@
393397
}
394398
#code-section select {
395399
width: auto;
396-
margin: 0;
400+
margin: 0 0 0 1rem;
397401
}
398402
footer {
399403
text-align: right;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "base-ex",
33
"version": "0.3.0",
4-
"description": "A collection of classes for data representation from Base16 (hex) to BasE91 written in JavaScript.",
4+
"description": "A collection of classes for data representation from Base16 (hex) to basE91 written in JavaScript.",
55
"main": "dist/BaseEx.cjs.js",
66
"module": "src/BaseEx.js",
77
"directories": {

0 commit comments

Comments
 (0)