From b035d1ac1efedcbda0a6c4ab0f2fbc7ab359dd8f Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Thu, 6 Mar 2025 09:18:07 +0100 Subject: [PATCH 1/4] Implement top-level cw object and bump v0.4 --- README.md | 37 +++++++++++++++++++++++++++++++++++-- examples/jsdelivr-demo.html | 10 +++++++--- package.json | 2 +- rollup.config.js | 5 +++-- src/index.ts | 13 +++++++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a1cece2..60ba8f9 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,27 @@ Install the package via `npm` or `yarn`: $ npm install cw ``` -You can then use: +You can use it in two ways: -```node +#### Default Import (Recommended) + +```javascript +import cw from "cw"; +console.log(cw.codes["C"]); // "-.-." +cw.play("hello world"); +``` + +#### Named Imports (For tree-shaking) + +```javascript +import { codes, play } from "cw"; +console.log(codes["C"]); // "-.-." +play("hello world"); +``` + +For CommonJS environments: + +```javascript const cw = require("cw"); console.log(cw.codes["C"]); // "-.-." ``` @@ -74,13 +92,28 @@ cw.play("abcd", { In cases of multiple plays, it is recommended to create a global `audioContext` object: ```js +// Initialize audio context once let actx = cw.initAudioContext({ tone: 600 }); // tone is passed here +// Reuse the audio context for multiple plays cw.play("abcd", { actx, wpm: 10 }); cw.play("efgh", { actx, wpm: 20 }); cw.play("ijkl", { actx, wpm: 30 }); ``` +For ES module environments, you can use the default import: + +```js +import cw from 'cw'; + +// Initialize audio context once +let actx = cw.initAudioContext({ tone: 600 }); + +// Use the cw object for all operations +cw.play("abcd", { actx, wpm: 10 }); +console.log(cw.codes["C"]); // "-.-." +``` + For more examples see the [examples/](examples/) directory. ### Testing diff --git a/examples/jsdelivr-demo.html b/examples/jsdelivr-demo.html index c6a0261..6d3f972 100644 --- a/examples/jsdelivr-demo.html +++ b/examples/jsdelivr-demo.html @@ -76,16 +76,20 @@

Example 3: Play Morse Code

How to Include cw.js in Your Project

Via jsDelivr CDN:

-
<script src="https://cdn.jsdelivr.net/npm/cw@0.3.0/dist/cw.min.js"></script>
+
<script src="https://cdn.jsdelivr.net/npm/cw@0.4.0/dist/cw.min.js"></script>

Via npm:

npm install cw

ES Module Import:

-
import { tdit, tfdit, codes, initAudioContext, play } from 'cw';
+
// Default import (recommended)
+import cw from 'cw';
+
+// OR named imports for tree-shaking
+import { tdit, tfdit, codes, initAudioContext, play } from 'cw';
- + diff --git a/rollup.config.js b/rollup.config.js index 776aae1..b36d632 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -15,7 +15,7 @@ export default [ file: pkg.browser, format: "umd", sourcemap: true, - exports: "auto" // Handle both named and default exports + exports: "default", }, plugins: [resolve(), commonjs(), typescript({ tsconfig: "./tsconfig.json" }), terser()], }, @@ -23,8 +23,8 @@ export default [ input: "src/index.ts", external: ["cw"], output: [ - { file: pkg.main, format: "cjs", sourcemap: true, exports: "auto" }, - { file: pkg.module, format: "es", sourcemap: true, exports: "auto" }, + { file: pkg.main, format: "cjs", sourcemap: true, exports: "default" }, + { file: pkg.module, format: "es", sourcemap: true, exports: "default" }, ], plugins: [typescript({ tsconfig: "./tsconfig.json" })], }, diff --git a/src/index.ts b/src/index.ts index b706e22..72990c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,17 +2,12 @@ import { tdit, tfdit } from "./timings.js"; import { codes } from "./codes.js"; import { initAudioContext, play } from "./audio.js"; -// Individual exports for backward compatibility and tree-shaking -export { tdit, tfdit, codes, initAudioContext, play }; - -// Create a unified cw object with all functionality const cw = { - tdit, - tfdit, - codes, - initAudioContext, - play + tdit, + tfdit, + codes, + initAudioContext, + play }; -// Default export for a more consistent API across all environments export default cw; From 4c015ca580458fb6905552a93c213e432172c261 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Thu, 6 Mar 2025 09:25:32 +0100 Subject: [PATCH 3/4] Fix imports in unit test --- test/test.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/test.js b/test/test.js index 24e93b3..7c1d7c7 100644 --- a/test/test.js +++ b/test/test.js @@ -1,28 +1,28 @@ import { expect } from 'chai'; -import { tdit, tfdit, codes } from '../dist/cw.esm.js'; +import cw from '../dist/cw.esm.js'; describe("cw", function () { describe("#tdit()", function () { it("should calculate the correct dit length in ms", function () { - expect(tdit(20)).to.equal(0.06); - expect(tdit(50)).to.equal(0.024); + expect(cw.tdit(20)).to.equal(0.06); + expect(cw.tdit(50)).to.equal(0.024); }); }); - + describe("#tfdit()", function () { it("should calculate the correct farnsworth dit length in ms", function () { - expect(tfdit(20, 10)).to.equal(0.21789473684210525); - expect(tfdit(20, 20)).to.equal(0.06); - expect(tfdit(20)).to.equal(0.06); + expect(cw.tfdit(20, 10)).to.equal(0.21789473684210525); + expect(cw.tfdit(20, 20)).to.equal(0.06); + expect(cw.tfdit(20)).to.equal(0.06); }); }); - + describe("#codes", function () { it("should provide an object mapping characters to morse code", function () { - expect(codes["C"]).to.equal("-.-."); - expect(codes["8"]).to.equal("---.."); - expect(codes["."]).to.equal(".-.-.-"); - expect(codes["w"]).to.be.undefined; + expect(cw.codes["C"]).to.equal("-.-."); + expect(cw.codes["8"]).to.equal("---.."); + expect(cw.codes["."]).to.equal(".-.-.-"); + expect(cw.codes["w"]).to.be.undefined; }); }); }); From 2ab209225717d716ead1ac98e1eda5fe96987d73 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Thu, 6 Mar 2025 09:30:00 +0100 Subject: [PATCH 4/4] Add docs in timings.ts --- src/timings.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/timings.ts b/src/timings.ts index 7b30172..21f988e 100644 --- a/src/timings.ts +++ b/src/timings.ts @@ -1,3 +1,24 @@ +/** + * Morse Code Timing Utilities + * + * This module provides functions for calculating Morse code timing parameters. + * + * Farnsworth Timing: + * Farnsworth timing is a method of sending Morse code where the individual characters + * are sent at a higher speed (wpm), but the spacing between characters and words is + * increased to achieve a lower effective speed (fwpm). This technique was developed by + * Russ Farnsworth (W6TTB) to help students learn Morse code more effectively. + * + * The idea behind Farnsworth timing is that learners can become accustomed to hearing + * and recognizing characters at higher speeds, while the extended spacing gives them + * more time to process each character before the next one arrives. As proficiency + * increases, the spacing can be gradually reduced until standard timing is achieved. + * + * In standard Morse timing, characters and spaces are proportional to the dit length. + * In Farnsworth timing, the characters maintain the same internal timing as they would + * at the higher speed, but the spaces between characters and words are extended. + */ + /** * Calculate the dit length in seconds based on words per minute * @param wpm Words per minute