@bendyline/squisq 0.1.2 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{themeLibrary-DySHPcgj.d.ts → Doc-1hmT1LZl.d.ts} +1 -42
- package/dist/__tests__/animationUtils.test.d.ts +2 -0
- package/dist/__tests__/animationUtils.test.d.ts.map +1 -0
- package/dist/__tests__/animationUtils.test.js +60 -0
- package/dist/__tests__/animationUtils.test.js.map +1 -0
- package/dist/__tests__/contentExtractor.test.d.ts +2 -0
- package/dist/__tests__/contentExtractor.test.d.ts.map +1 -0
- package/dist/__tests__/contentExtractor.test.js +151 -0
- package/dist/__tests__/contentExtractor.test.js.map +1 -0
- package/dist/__tests__/geohash.test.d.ts +2 -0
- package/dist/__tests__/geohash.test.d.ts.map +1 -0
- package/dist/__tests__/geohash.test.js +87 -0
- package/dist/__tests__/geohash.test.js.map +1 -0
- package/dist/__tests__/getLayers.test.d.ts +2 -0
- package/dist/__tests__/getLayers.test.d.ts.map +1 -0
- package/dist/__tests__/getLayers.test.js +167 -0
- package/dist/__tests__/getLayers.test.js.map +1 -0
- package/dist/__tests__/haversine.test.d.ts +2 -0
- package/dist/__tests__/haversine.test.d.ts.map +1 -0
- package/dist/__tests__/haversine.test.js +56 -0
- package/dist/__tests__/haversine.test.js.map +1 -0
- package/dist/__tests__/localForageAdapter.test.d.ts +8 -0
- package/dist/__tests__/localForageAdapter.test.d.ts.map +1 -0
- package/dist/__tests__/localForageAdapter.test.js +130 -0
- package/dist/__tests__/localForageAdapter.test.js.map +1 -0
- package/dist/__tests__/markdown.test.d.ts +2 -0
- package/dist/__tests__/markdown.test.d.ts.map +1 -0
- package/dist/__tests__/markdown.test.js +654 -0
- package/dist/__tests__/markdown.test.js.map +1 -0
- package/dist/__tests__/markdownToDoc.test.d.ts +2 -0
- package/dist/__tests__/markdownToDoc.test.d.ts.map +1 -0
- package/dist/__tests__/markdownToDoc.test.js +313 -0
- package/dist/__tests__/markdownToDoc.test.js.map +1 -0
- package/dist/__tests__/seededRandom.test.d.ts +2 -0
- package/dist/__tests__/seededRandom.test.d.ts.map +1 -0
- package/dist/__tests__/seededRandom.test.js +125 -0
- package/dist/__tests__/seededRandom.test.js.map +1 -0
- package/dist/__tests__/slideshowGenerator.test.d.ts +2 -0
- package/dist/__tests__/slideshowGenerator.test.d.ts.map +1 -0
- package/dist/__tests__/slideshowGenerator.test.js +114 -0
- package/dist/__tests__/slideshowGenerator.test.js.map +1 -0
- package/dist/__tests__/storage.test.d.ts +2 -0
- package/dist/__tests__/storage.test.d.ts.map +1 -0
- package/dist/__tests__/storage.test.js +51 -0
- package/dist/__tests__/storage.test.js.map +1 -0
- package/dist/__tests__/templates.test.d.ts +2 -0
- package/dist/__tests__/templates.test.d.ts.map +1 -0
- package/dist/__tests__/templates.test.js +217 -0
- package/dist/__tests__/templates.test.js.map +1 -0
- package/dist/__tests__/timing.test.d.ts +2 -0
- package/dist/__tests__/timing.test.d.ts.map +1 -0
- package/dist/__tests__/timing.test.js +131 -0
- package/dist/__tests__/timing.test.js.map +1 -0
- package/dist/chunk-3E5F2XMR.js +129 -0
- package/dist/chunk-3E5F2XMR.js.map +1 -0
- package/dist/chunk-6YJPI6VI.js +35 -0
- package/dist/chunk-6YJPI6VI.js.map +1 -0
- package/dist/chunk-FVTQSNR7.js +100 -0
- package/dist/chunk-FVTQSNR7.js.map +1 -0
- package/dist/chunk-V543AI2F.js +1 -0
- package/dist/chunk-V543AI2F.js.map +1 -0
- package/dist/chunk-VP5SZTZ2.js +792 -0
- package/dist/chunk-VP5SZTZ2.js.map +1 -0
- package/dist/doc/docToMarkdown.d.ts +41 -0
- package/dist/doc/docToMarkdown.d.ts.map +1 -0
- package/dist/doc/docToMarkdown.js +88 -0
- package/dist/doc/docToMarkdown.js.map +1 -0
- package/dist/doc/getLayers.d.ts +65 -0
- package/dist/doc/getLayers.d.ts.map +1 -0
- package/dist/doc/getLayers.js +103 -0
- package/dist/doc/getLayers.js.map +1 -0
- package/dist/doc/index.d.ts +9 -3
- package/dist/doc/index.d.ts.map +1 -0
- package/dist/doc/index.js +6 -125
- package/dist/doc/index.js.map +1 -1
- package/dist/doc/markdownToDoc.d.ts +68 -0
- package/dist/doc/markdownToDoc.d.ts.map +1 -0
- package/dist/doc/markdownToDoc.js +225 -0
- package/dist/doc/markdownToDoc.js.map +1 -0
- package/dist/doc/templates/accentImage.d.ts +59 -0
- package/dist/doc/templates/accentImage.d.ts.map +1 -0
- package/dist/doc/templates/accentImage.js +275 -0
- package/dist/doc/templates/accentImage.js.map +1 -0
- package/dist/doc/templates/captionUtils.d.ts +20 -0
- package/dist/doc/templates/captionUtils.d.ts.map +1 -0
- package/dist/doc/templates/captionUtils.js +29 -0
- package/dist/doc/templates/captionUtils.js.map +1 -0
- package/dist/doc/templates/comparisonBar.d.ts +13 -0
- package/dist/doc/templates/comparisonBar.d.ts.map +1 -0
- package/dist/doc/templates/comparisonBar.js +127 -0
- package/dist/doc/templates/comparisonBar.js.map +1 -0
- package/dist/doc/templates/coverBlock.d.ts +51 -0
- package/dist/doc/templates/coverBlock.d.ts.map +1 -0
- package/dist/doc/templates/coverBlock.js +125 -0
- package/dist/doc/templates/coverBlock.js.map +1 -0
- package/dist/doc/templates/dateEvent.d.ts +15 -0
- package/dist/doc/templates/dateEvent.d.ts.map +1 -0
- package/dist/doc/templates/dateEvent.js +110 -0
- package/dist/doc/templates/dateEvent.js.map +1 -0
- package/dist/doc/templates/definitionCard.d.ts +13 -0
- package/dist/doc/templates/definitionCard.d.ts.map +1 -0
- package/dist/doc/templates/definitionCard.js +114 -0
- package/dist/doc/templates/definitionCard.js.map +1 -0
- package/dist/doc/templates/factCard.d.ts +15 -0
- package/dist/doc/templates/factCard.d.ts.map +1 -0
- package/dist/doc/templates/factCard.js +100 -0
- package/dist/doc/templates/factCard.js.map +1 -0
- package/dist/doc/templates/fullBleedQuote.d.ts +13 -0
- package/dist/doc/templates/fullBleedQuote.d.ts.map +1 -0
- package/dist/doc/templates/fullBleedQuote.js +54 -0
- package/dist/doc/templates/fullBleedQuote.js.map +1 -0
- package/dist/doc/templates/imageWithCaption.d.ts +13 -0
- package/dist/doc/templates/imageWithCaption.d.ts.map +1 -0
- package/dist/doc/templates/imageWithCaption.js +142 -0
- package/dist/doc/templates/imageWithCaption.js.map +1 -0
- package/dist/doc/templates/index.d.ts +104 -0
- package/dist/doc/templates/index.d.ts.map +1 -0
- package/dist/doc/templates/index.js +442 -0
- package/dist/doc/templates/index.js.map +1 -0
- package/dist/doc/templates/listBlock.d.ts +13 -0
- package/dist/doc/templates/listBlock.d.ts.map +1 -0
- package/dist/doc/templates/listBlock.js +89 -0
- package/dist/doc/templates/listBlock.js.map +1 -0
- package/dist/doc/templates/mapBlock.d.ts +17 -0
- package/dist/doc/templates/mapBlock.d.ts.map +1 -0
- package/dist/doc/templates/mapBlock.js +112 -0
- package/dist/doc/templates/mapBlock.js.map +1 -0
- package/dist/doc/templates/persistentLayers.d.ts +39 -0
- package/dist/doc/templates/persistentLayers.d.ts.map +1 -0
- package/dist/doc/templates/persistentLayers.js +447 -0
- package/dist/doc/templates/persistentLayers.js.map +1 -0
- package/dist/doc/templates/photoGrid.d.ts +20 -0
- package/dist/doc/templates/photoGrid.d.ts.map +1 -0
- package/dist/doc/templates/photoGrid.js +172 -0
- package/dist/doc/templates/photoGrid.js.map +1 -0
- package/dist/doc/templates/pullQuote.d.ts +13 -0
- package/dist/doc/templates/pullQuote.d.ts.map +1 -0
- package/dist/doc/templates/pullQuote.js +109 -0
- package/dist/doc/templates/pullQuote.js.map +1 -0
- package/dist/doc/templates/quoteBlock.d.ts +15 -0
- package/dist/doc/templates/quoteBlock.d.ts.map +1 -0
- package/dist/doc/templates/quoteBlock.js +97 -0
- package/dist/doc/templates/quoteBlock.js.map +1 -0
- package/dist/doc/templates/sectionHeader.d.ts +14 -0
- package/dist/doc/templates/sectionHeader.d.ts.map +1 -0
- package/dist/doc/templates/sectionHeader.js +96 -0
- package/dist/doc/templates/sectionHeader.js.map +1 -0
- package/dist/doc/templates/statHighlight.d.ts +15 -0
- package/dist/doc/templates/statHighlight.d.ts.map +1 -0
- package/dist/doc/templates/statHighlight.js +99 -0
- package/dist/doc/templates/statHighlight.js.map +1 -0
- package/dist/doc/templates/titleBlock.d.ts +13 -0
- package/dist/doc/templates/titleBlock.d.ts.map +1 -0
- package/dist/doc/templates/titleBlock.js +92 -0
- package/dist/doc/templates/titleBlock.js.map +1 -0
- package/dist/doc/templates/twoColumn.d.ts +15 -0
- package/dist/doc/templates/twoColumn.d.ts.map +1 -0
- package/dist/doc/templates/twoColumn.js +219 -0
- package/dist/doc/templates/twoColumn.js.map +1 -0
- package/dist/doc/templates/videoPullQuote.d.ts +20 -0
- package/dist/doc/templates/videoPullQuote.d.ts.map +1 -0
- package/dist/doc/templates/videoPullQuote.js +118 -0
- package/dist/doc/templates/videoPullQuote.js.map +1 -0
- package/dist/doc/templates/videoWithCaption.d.ts +20 -0
- package/dist/doc/templates/videoWithCaption.d.ts.map +1 -0
- package/dist/doc/templates/videoWithCaption.js +104 -0
- package/dist/doc/templates/videoWithCaption.js.map +1 -0
- package/dist/doc/utils/animationUtils.d.ts +34 -0
- package/dist/doc/utils/animationUtils.d.ts.map +1 -0
- package/dist/doc/utils/animationUtils.js +110 -0
- package/dist/doc/utils/animationUtils.js.map +1 -0
- package/dist/doc/utils/themeUtils.d.ts +55 -0
- package/dist/doc/utils/themeUtils.d.ts.map +1 -0
- package/dist/doc/utils/themeUtils.js +109 -0
- package/dist/doc/utils/themeUtils.js.map +1 -0
- package/dist/generate/contentExtractor.d.ts +112 -0
- package/dist/generate/contentExtractor.d.ts.map +1 -0
- package/dist/generate/contentExtractor.js +638 -0
- package/dist/generate/contentExtractor.js.map +1 -0
- package/dist/generate/index.d.ts +7 -0
- package/dist/generate/index.d.ts.map +1 -0
- package/dist/generate/index.js +4 -0
- package/dist/generate/index.js.map +1 -0
- package/dist/generate/slideshowGenerator.d.ts +72 -0
- package/dist/generate/slideshowGenerator.d.ts.map +1 -0
- package/dist/generate/slideshowGenerator.js +159 -0
- package/dist/generate/slideshowGenerator.js.map +1 -0
- package/dist/generate/templateMapper.d.ts +23 -0
- package/dist/generate/templateMapper.d.ts.map +1 -0
- package/dist/generate/templateMapper.js +114 -0
- package/dist/generate/templateMapper.js.map +1 -0
- package/dist/index.d.ts +9 -8
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -216
- package/dist/index.js.map +1 -1
- package/dist/markdown/convert.d.ts +71 -0
- package/dist/markdown/convert.d.ts.map +1 -0
- package/dist/markdown/convert.js +746 -0
- package/dist/markdown/convert.js.map +1 -0
- package/dist/markdown/htmlParse.d.ts +26 -0
- package/dist/markdown/htmlParse.d.ts.map +1 -0
- package/dist/markdown/htmlParse.js +195 -0
- package/dist/markdown/htmlParse.js.map +1 -0
- package/dist/markdown/index.d.ts +19 -225
- package/dist/markdown/index.d.ts.map +1 -0
- package/dist/markdown/index.js +33 -32
- package/dist/markdown/index.js.map +1 -1
- package/dist/markdown/parse.d.ts +27 -0
- package/dist/markdown/parse.d.ts.map +1 -0
- package/dist/markdown/parse.js +90 -0
- package/dist/markdown/parse.js.map +1 -0
- package/dist/markdown/stringify.d.ts +32 -0
- package/dist/markdown/stringify.d.ts.map +1 -0
- package/dist/markdown/stringify.js +108 -0
- package/dist/markdown/stringify.js.map +1 -0
- package/dist/markdown/types.d.ts +496 -0
- package/dist/markdown/types.d.ts.map +1 -0
- package/dist/markdown/types.js +17 -0
- package/dist/markdown/types.js.map +1 -0
- package/dist/markdown/utils.d.ts +78 -0
- package/dist/markdown/utils.d.ts.map +1 -0
- package/dist/markdown/utils.js +149 -0
- package/dist/markdown/utils.js.map +1 -0
- package/dist/random/SeededRandom.d.ts +50 -0
- package/dist/random/SeededRandom.d.ts.map +1 -0
- package/dist/random/SeededRandom.js +115 -0
- package/dist/random/SeededRandom.js.map +1 -0
- package/dist/random/index.d.ts +2 -0
- package/dist/random/index.d.ts.map +1 -0
- package/dist/random/index.js +2 -0
- package/dist/random/index.js.map +1 -0
- package/dist/schemas/BlockTemplates.d.ts +583 -0
- package/dist/schemas/BlockTemplates.d.ts.map +1 -0
- package/dist/schemas/BlockTemplates.js +72 -0
- package/dist/schemas/BlockTemplates.js.map +1 -0
- package/dist/schemas/Doc.d.ts +411 -0
- package/dist/schemas/Doc.d.ts.map +1 -0
- package/dist/schemas/Doc.js +90 -0
- package/dist/schemas/Doc.js.map +1 -0
- package/dist/schemas/LayoutStrategy.d.ts +84 -0
- package/dist/schemas/LayoutStrategy.d.ts.map +1 -0
- package/dist/schemas/LayoutStrategy.js +123 -0
- package/dist/schemas/LayoutStrategy.js.map +1 -0
- package/dist/schemas/MediaProvider.d.ts +65 -0
- package/dist/schemas/MediaProvider.d.ts.map +1 -0
- package/dist/schemas/MediaProvider.js +13 -0
- package/dist/schemas/MediaProvider.js.map +1 -0
- package/dist/schemas/Theme.d.ts +163 -0
- package/dist/schemas/Theme.d.ts.map +1 -0
- package/dist/schemas/Theme.js +62 -0
- package/dist/schemas/Theme.js.map +1 -0
- package/dist/schemas/Types.d.ts +14 -0
- package/dist/schemas/Types.d.ts.map +1 -0
- package/dist/schemas/Types.js +5 -0
- package/dist/schemas/Types.js.map +1 -0
- package/dist/schemas/Viewport.d.ts +74 -0
- package/dist/schemas/Viewport.d.ts.map +1 -0
- package/dist/schemas/Viewport.js +63 -0
- package/dist/schemas/Viewport.js.map +1 -0
- package/dist/schemas/index.d.ts +10 -70
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +7 -60
- package/dist/schemas/index.js.map +1 -1
- package/dist/schemas/themeLibrary.d.ts +41 -0
- package/dist/schemas/themeLibrary.d.ts.map +1 -0
- package/dist/schemas/themeLibrary.js +466 -0
- package/dist/schemas/themeLibrary.js.map +1 -0
- package/dist/spatial/Geohash.d.ts +85 -0
- package/dist/spatial/Geohash.d.ts.map +1 -0
- package/dist/spatial/Geohash.js +142 -0
- package/dist/spatial/Geohash.js.map +1 -0
- package/dist/spatial/Haversine.d.ts +24 -0
- package/dist/spatial/Haversine.d.ts.map +1 -0
- package/dist/spatial/Haversine.js +48 -0
- package/dist/spatial/Haversine.js.map +1 -0
- package/dist/spatial/index.d.ts +3 -111
- package/dist/spatial/index.d.ts.map +1 -0
- package/dist/spatial/index.js +2 -24
- package/dist/spatial/index.js.map +1 -1
- package/dist/storage/LocalForageAdapter.d.ts +36 -0
- package/dist/storage/LocalForageAdapter.d.ts.map +1 -0
- package/dist/storage/LocalForageAdapter.js +82 -0
- package/dist/storage/LocalForageAdapter.js.map +1 -0
- package/dist/storage/LocalStorageAdapter.d.ts +16 -0
- package/dist/storage/LocalStorageAdapter.d.ts.map +1 -0
- package/dist/storage/LocalStorageAdapter.js +50 -0
- package/dist/storage/LocalStorageAdapter.js.map +1 -0
- package/dist/storage/MemoryStorageAdapter.d.ts +15 -0
- package/dist/storage/MemoryStorageAdapter.d.ts.map +1 -0
- package/dist/storage/MemoryStorageAdapter.js +34 -0
- package/dist/storage/MemoryStorageAdapter.js.map +1 -0
- package/dist/storage/Storage.d.ts +23 -0
- package/dist/storage/Storage.d.ts.map +1 -0
- package/dist/storage/Storage.js +9 -0
- package/dist/storage/Storage.js.map +1 -0
- package/dist/storage/index.d.ts +6 -91
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +3 -10
- package/dist/storage/index.js.map +1 -1
- package/dist/story/index.d.ts +3 -2
- package/dist/themeLibrary-DR1VRo-8.d.ts +44 -0
- package/dist/timing/index.d.ts +4 -0
- package/dist/timing/index.d.ts.map +1 -0
- package/dist/timing/index.js +3 -0
- package/dist/timing/index.js.map +1 -0
- package/dist/timing/narrationTiming.d.ts +54 -0
- package/dist/timing/narrationTiming.d.ts.map +1 -0
- package/dist/timing/narrationTiming.js +190 -0
- package/dist/timing/narrationTiming.js.map +1 -0
- package/dist/timing/readingTime.d.ts +54 -0
- package/dist/timing/readingTime.d.ts.map +1 -0
- package/dist/timing/readingTime.js +49 -0
- package/dist/timing/readingTime.js.map +1 -0
- package/package.json +17 -3
- package/src/__tests__/contentExtractor.test.ts +183 -0
- package/src/__tests__/seededRandom.test.ts +142 -0
- package/src/__tests__/slideshowGenerator.test.ts +128 -0
- package/src/__tests__/timing.test.ts +167 -0
- package/src/generate/contentExtractor.ts +808 -0
- package/src/generate/index.ts +21 -0
- package/src/generate/slideshowGenerator.ts +273 -0
- package/src/generate/templateMapper.ts +170 -0
- package/src/index.ts +3 -0
- package/src/random/SeededRandom.ts +127 -0
- package/src/random/index.ts +1 -0
- package/src/timing/index.ts +17 -0
- package/src/timing/narrationTiming.ts +220 -0
- package/src/timing/readingTime.ts +94 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/timing/narrationTiming.ts
|
|
2
|
+
var NUMBER_SPEAKING_MULTIPLIER = 1.3;
|
|
3
|
+
var COMMA_PAUSE_PENALTY = 0.3;
|
|
4
|
+
var DEFAULT_WORDS_PER_SECOND = 2.5;
|
|
5
|
+
function estimateSpokenWordCount(token) {
|
|
6
|
+
const cleaned = token.replace(/,/g, "");
|
|
7
|
+
const num = parseFloat(cleaned);
|
|
8
|
+
if (isNaN(num) || !isFinite(num)) {
|
|
9
|
+
return 1;
|
|
10
|
+
}
|
|
11
|
+
return estimateNumberWordCount(cleaned, num) * NUMBER_SPEAKING_MULTIPLIER;
|
|
12
|
+
}
|
|
13
|
+
function estimateNumberWordCount(cleaned, num) {
|
|
14
|
+
if (cleaned.includes(".")) {
|
|
15
|
+
const [whole, decimal] = cleaned.split(".");
|
|
16
|
+
const wholeNum = parseFloat(whole) || 0;
|
|
17
|
+
return estimateNumberWordCount(whole, wholeNum) + 1 + decimal.length;
|
|
18
|
+
}
|
|
19
|
+
const absNum = Math.abs(num);
|
|
20
|
+
let words = num < 0 ? 1 : 0;
|
|
21
|
+
if (absNum === 0) return 1;
|
|
22
|
+
if (absNum >= 1e3 && absNum <= 2099 && Number.isInteger(absNum)) {
|
|
23
|
+
const yearStr = absNum.toString();
|
|
24
|
+
if (yearStr.length === 4) {
|
|
25
|
+
const firstTwo = parseInt(yearStr.slice(0, 2));
|
|
26
|
+
const lastTwo = parseInt(yearStr.slice(2));
|
|
27
|
+
if (firstTwo >= 10 && firstTwo <= 19) {
|
|
28
|
+
words += 1;
|
|
29
|
+
} else if (firstTwo >= 20 && firstTwo <= 99) {
|
|
30
|
+
words += firstTwo % 10 === 0 ? 1 : 2;
|
|
31
|
+
} else {
|
|
32
|
+
words += 2;
|
|
33
|
+
}
|
|
34
|
+
if (lastTwo === 0) {
|
|
35
|
+
if (firstTwo >= 10 && firstTwo <= 99 && firstTwo !== 20) {
|
|
36
|
+
words += 1;
|
|
37
|
+
}
|
|
38
|
+
} else if (lastTwo >= 1 && lastTwo <= 9) {
|
|
39
|
+
words += 2;
|
|
40
|
+
} else if (lastTwo >= 10 && lastTwo <= 19) {
|
|
41
|
+
words += 1;
|
|
42
|
+
} else {
|
|
43
|
+
words += lastTwo % 10 === 0 ? 1 : 2;
|
|
44
|
+
}
|
|
45
|
+
return Math.max(words, 2);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let remaining = absNum;
|
|
49
|
+
if (remaining >= 1e9) {
|
|
50
|
+
const billions = Math.floor(remaining / 1e9);
|
|
51
|
+
words += estimateNumberWordCount(String(billions), billions);
|
|
52
|
+
words += 1;
|
|
53
|
+
remaining %= 1e9;
|
|
54
|
+
}
|
|
55
|
+
if (remaining >= 1e6) {
|
|
56
|
+
const millions = Math.floor(remaining / 1e6);
|
|
57
|
+
words += estimateNumberWordCount(String(millions), millions);
|
|
58
|
+
words += 1;
|
|
59
|
+
remaining %= 1e6;
|
|
60
|
+
}
|
|
61
|
+
if (remaining >= 1e3) {
|
|
62
|
+
const thousands = Math.floor(remaining / 1e3);
|
|
63
|
+
words += estimateNumberWordCount(String(thousands), thousands);
|
|
64
|
+
words += 1;
|
|
65
|
+
remaining %= 1e3;
|
|
66
|
+
}
|
|
67
|
+
if (remaining >= 100) {
|
|
68
|
+
words += 2;
|
|
69
|
+
remaining %= 100;
|
|
70
|
+
}
|
|
71
|
+
if (remaining >= 1) {
|
|
72
|
+
if (remaining >= 20) {
|
|
73
|
+
words += remaining % 10 === 0 ? 1 : 2;
|
|
74
|
+
} else {
|
|
75
|
+
words += 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return Math.max(words, 1);
|
|
79
|
+
}
|
|
80
|
+
function countSpokenWords(text) {
|
|
81
|
+
const tokens = text.split(/\s+/).filter((w) => w.length > 0);
|
|
82
|
+
let count = 0;
|
|
83
|
+
for (const token of tokens) {
|
|
84
|
+
const stripped = token.replace(/^[.,!?;:'"()[\]{}]+|[.,!?;:'"()[\]{}]+$/g, "");
|
|
85
|
+
count += estimateSpokenWordCount(stripped);
|
|
86
|
+
let punctuationCommaCount = 0;
|
|
87
|
+
for (let i = 0; i < token.length; i++) {
|
|
88
|
+
if (token[i] === ",") {
|
|
89
|
+
const prev = i > 0 ? token[i - 1] : "";
|
|
90
|
+
const next = i < token.length - 1 ? token[i + 1] : "";
|
|
91
|
+
const prevIsDigit = prev >= "0" && prev <= "9";
|
|
92
|
+
const nextIsDigit = next >= "0" && next <= "9";
|
|
93
|
+
if (!(prevIsDigit && nextIsDigit)) {
|
|
94
|
+
punctuationCommaCount++;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
count += punctuationCommaCount * COMMA_PAUSE_PENALTY;
|
|
99
|
+
}
|
|
100
|
+
return count;
|
|
101
|
+
}
|
|
102
|
+
function estimateTimeFromText(text, charOffset, totalDuration) {
|
|
103
|
+
const textBefore = text.slice(0, charOffset);
|
|
104
|
+
const spokenWordsBefore = countSpokenWords(textBefore);
|
|
105
|
+
const totalSpokenWords = countSpokenWords(text);
|
|
106
|
+
if (totalSpokenWords === 0) return 0;
|
|
107
|
+
return spokenWordsBefore / totalSpokenWords * totalDuration;
|
|
108
|
+
}
|
|
109
|
+
function calculatePrefixDuration(prefix, wordsPerSecond = DEFAULT_WORDS_PER_SECOND) {
|
|
110
|
+
if (!prefix) return 0;
|
|
111
|
+
const words = prefix.split(/\s+/).filter((w) => w.length > 0);
|
|
112
|
+
const newlineCount = (prefix.match(/\n\n/g) || []).length;
|
|
113
|
+
const pauseTime = newlineCount * 0.5;
|
|
114
|
+
return words.length / wordsPerSecond + pauseTime;
|
|
115
|
+
}
|
|
116
|
+
function estimateNarrationDuration(text, wordsPerSecond = DEFAULT_WORDS_PER_SECOND) {
|
|
117
|
+
const spokenWords = countSpokenWords(text);
|
|
118
|
+
return spokenWords / wordsPerSecond;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export {
|
|
122
|
+
DEFAULT_WORDS_PER_SECOND,
|
|
123
|
+
estimateSpokenWordCount,
|
|
124
|
+
countSpokenWords,
|
|
125
|
+
estimateTimeFromText,
|
|
126
|
+
calculatePrefixDuration,
|
|
127
|
+
estimateNarrationDuration
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=chunk-3E5F2XMR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/timing/narrationTiming.ts"],"sourcesContent":["/**\n * Narration timing estimation.\n *\n * Estimates how long text takes to speak aloud, accounting for:\n * - Numbers spoken as multiple words (e.g., \"1910\" → \"nineteen ten\")\n * - Pauses at commas\n * - Year pronunciation conventions (1000–2099)\n *\n * Ported from qualla-internal's AudioTiming.ts (pure functions, no Node.js deps).\n */\n\n/** Numbers are spoken more slowly than regular words (1.3× multiplier). */\nconst NUMBER_SPEAKING_MULTIPLIER = 1.3;\n\n/** Comma pause in \"word equivalents\" — speakers pause ~0.3 words at commas. */\nconst COMMA_PAUSE_PENALTY = 0.3;\n\n/** Default speaking rate in words per second (~150 WPM). */\nexport const DEFAULT_WORDS_PER_SECOND = 2.5;\n\n/**\n * Estimate how many \"spoken word equivalents\" a single token represents.\n * Numbers get expanded (e.g., \"175,000\" → ~5 words × 1.3 multiplier).\n */\nexport function estimateSpokenWordCount(token: string): number {\n const cleaned = token.replace(/,/g, '');\n const num = parseFloat(cleaned);\n\n if (isNaN(num) || !isFinite(num)) {\n return 1; // Not a number — count as 1 word\n }\n\n return estimateNumberWordCount(cleaned, num) * NUMBER_SPEAKING_MULTIPLIER;\n}\n\n/**\n * Raw word count for a number (before the speaking multiplier).\n */\nfunction estimateNumberWordCount(cleaned: string, num: number): number {\n // Handle decimals: \"3.14\" → words(\"3\") + \"point\" + 2 digits\n if (cleaned.includes('.')) {\n const [whole, decimal] = cleaned.split('.');\n const wholeNum = parseFloat(whole) || 0;\n return estimateNumberWordCount(whole, wholeNum) + 1 + decimal.length;\n }\n\n const absNum = Math.abs(num);\n let words = num < 0 ? 1 : 0; // \"negative\" / \"minus\"\n\n if (absNum === 0) return 1; // \"zero\"\n\n // Years (1000–2099) are spoken as two two-digit groups:\n // \"1910\" → \"nineteen ten\", \"2024\" → \"twenty twenty-four\"\n if (absNum >= 1000 && absNum <= 2099 && Number.isInteger(absNum)) {\n const yearStr = absNum.toString();\n if (yearStr.length === 4) {\n const firstTwo = parseInt(yearStr.slice(0, 2));\n const lastTwo = parseInt(yearStr.slice(2));\n\n // First part\n if (firstTwo >= 10 && firstTwo <= 19) {\n words += 1;\n } else if (firstTwo >= 20 && firstTwo <= 99) {\n words += firstTwo % 10 === 0 ? 1 : 2;\n } else {\n words += 2; // \"two thousand\"\n }\n\n // Second part\n if (lastTwo === 0) {\n if (firstTwo >= 10 && firstTwo <= 99 && firstTwo !== 20) {\n words += 1; // \"hundred\"\n }\n } else if (lastTwo >= 1 && lastTwo <= 9) {\n words += 2; // \"oh five\"\n } else if (lastTwo >= 10 && lastTwo <= 19) {\n words += 1;\n } else {\n words += lastTwo % 10 === 0 ? 1 : 2;\n }\n\n return Math.max(words, 2);\n }\n }\n\n // General number handling — each magnitude group adds words\n let remaining = absNum;\n\n if (remaining >= 1_000_000_000) {\n const billions = Math.floor(remaining / 1_000_000_000);\n words += estimateNumberWordCount(String(billions), billions);\n words += 1; // \"billion\"\n remaining %= 1_000_000_000;\n }\n\n if (remaining >= 1_000_000) {\n const millions = Math.floor(remaining / 1_000_000);\n words += estimateNumberWordCount(String(millions), millions);\n words += 1; // \"million\"\n remaining %= 1_000_000;\n }\n\n if (remaining >= 1_000) {\n const thousands = Math.floor(remaining / 1_000);\n words += estimateNumberWordCount(String(thousands), thousands);\n words += 1; // \"thousand\"\n remaining %= 1_000;\n }\n\n if (remaining >= 100) {\n words += 2; // digit + \"hundred\"\n remaining %= 100;\n }\n\n if (remaining >= 1) {\n if (remaining >= 20) {\n words += remaining % 10 === 0 ? 1 : 2;\n } else {\n words += 1; // 1–19\n }\n }\n\n return Math.max(words, 1);\n}\n\n/**\n * Count \"spoken word equivalents\" in a block of text,\n * expanding numbers and adding comma pauses.\n */\nexport function countSpokenWords(text: string): number {\n const tokens = text.split(/\\s+/).filter((w) => w.length > 0);\n let count = 0;\n\n for (const token of tokens) {\n // Strip punctuation only at token boundaries so internal punctuation\n // (e.g., decimals like \"3.14\" or digit-grouping commas \"1,000\") is preserved.\n const stripped = token.replace(/^[.,!?;:'\"()[\\]{}]+|[.,!?;:'\"()[\\]{}]+$/g, '');\n count += estimateSpokenWordCount(stripped);\n\n // Count only punctuation commas for pauses: exclude digit-grouping commas\n // (i.e., commas with digits on both sides, like in \"1,234\").\n let punctuationCommaCount = 0;\n for (let i = 0; i < token.length; i++) {\n if (token[i] === ',') {\n const prev = i > 0 ? token[i - 1] : '';\n const next = i < token.length - 1 ? token[i + 1] : '';\n const prevIsDigit = prev >= '0' && prev <= '9';\n const nextIsDigit = next >= '0' && next <= '9';\n if (!(prevIsDigit && nextIsDigit)) {\n punctuationCommaCount++;\n }\n }\n }\n count += punctuationCommaCount * COMMA_PAUSE_PENALTY;\n }\n\n return count;\n}\n\n/**\n * Estimate audio time at a character offset using word-proportion mapping.\n *\n * Given the full source text and a character position, returns the\n * estimated time (in seconds) at which that position would be spoken.\n *\n * @param text - Full source text\n * @param charOffset - Character offset to estimate timing for\n * @param totalDuration - Total audio duration in seconds\n * @returns Estimated time in seconds\n */\nexport function estimateTimeFromText(\n text: string,\n charOffset: number,\n totalDuration: number,\n): number {\n const textBefore = text.slice(0, charOffset);\n const spokenWordsBefore = countSpokenWords(textBefore);\n const totalSpokenWords = countSpokenWords(text);\n\n if (totalSpokenWords === 0) return 0;\n return (spokenWordsBefore / totalSpokenWords) * totalDuration;\n}\n\n/**\n * Estimate how long a spoken prefix adds to audio duration.\n *\n * When audio includes a spoken title or header before the main content,\n * this calculates the extra duration to offset timing calculations.\n *\n * @param prefix - Prefix text (e.g., an article title spoken before the body)\n * @param wordsPerSecond - Speaking rate (default: 2.5)\n * @returns Estimated duration in seconds\n */\nexport function calculatePrefixDuration(\n prefix: string,\n wordsPerSecond: number = DEFAULT_WORDS_PER_SECOND,\n): number {\n if (!prefix) return 0;\n\n const words = prefix.split(/\\s+/).filter((w) => w.length > 0);\n const newlineCount = (prefix.match(/\\n\\n/g) || []).length;\n const pauseTime = newlineCount * 0.5;\n\n return words.length / wordsPerSecond + pauseTime;\n}\n\n/**\n * Estimate narration duration for a block of text.\n *\n * @param text - The text to estimate\n * @param wordsPerSecond - Speaking rate (default: 2.5, i.e. ~150 WPM)\n * @returns Duration in seconds\n */\nexport function estimateNarrationDuration(\n text: string,\n wordsPerSecond: number = DEFAULT_WORDS_PER_SECOND,\n): number {\n const spokenWords = countSpokenWords(text);\n return spokenWords / wordsPerSecond;\n}\n"],"mappings":";AAYA,IAAM,6BAA6B;AAGnC,IAAM,sBAAsB;AAGrB,IAAM,2BAA2B;AAMjC,SAAS,wBAAwB,OAAuB;AAC7D,QAAM,UAAU,MAAM,QAAQ,MAAM,EAAE;AACtC,QAAM,MAAM,WAAW,OAAO;AAE9B,MAAI,MAAM,GAAG,KAAK,CAAC,SAAS,GAAG,GAAG;AAChC,WAAO;AAAA,EACT;AAEA,SAAO,wBAAwB,SAAS,GAAG,IAAI;AACjD;AAKA,SAAS,wBAAwB,SAAiB,KAAqB;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;AAC3C,QAAM,oBAAoB,iBAAiB,UAAU;AACrD,QAAM,mBAAmB,iBAAiB,IAAI;AAE9C,MAAI,qBAAqB,EAAG,QAAO;AACnC,SAAQ,oBAAoB,mBAAoB;AAClD;AAYO,SAAS,wBACd,QACA,iBAAyB,0BACjB;AACR,MAAI,CAAC,OAAQ,QAAO;AAEpB,QAAM,QAAQ,OAAO,MAAM,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC;AAC5D,QAAM,gBAAgB,OAAO,MAAM,OAAO,KAAK,CAAC,GAAG;AACnD,QAAM,YAAY,eAAe;AAEjC,SAAO,MAAM,SAAS,iBAAiB;AACzC;AASO,SAAS,0BACd,MACA,iBAAyB,0BACjB;AACR,QAAM,cAAc,iBAAiB,IAAI;AACzC,SAAO,cAAc;AACvB;","names":[]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_WORDS_PER_SECOND,
|
|
3
|
+
countSpokenWords
|
|
4
|
+
} from "./chunk-3E5F2XMR.js";
|
|
5
|
+
|
|
6
|
+
// src/timing/readingTime.ts
|
|
7
|
+
var DEFAULT_READING_WPM = 200;
|
|
8
|
+
function estimateReadingTime(text, options) {
|
|
9
|
+
const configuredWpm = options?.wordsPerMinute;
|
|
10
|
+
const wpm = typeof configuredWpm === "number" && configuredWpm > 0 ? configuredWpm : DEFAULT_READING_WPM;
|
|
11
|
+
const words = text.split(/\s+/).filter((w) => w.length > 0).length;
|
|
12
|
+
const minutes = words / wpm;
|
|
13
|
+
return {
|
|
14
|
+
words,
|
|
15
|
+
minutes,
|
|
16
|
+
seconds: Math.round(minutes * 60)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function estimateNarrationTime(text, options) {
|
|
20
|
+
const providedWps = options?.wordsPerSecond;
|
|
21
|
+
const wps = typeof providedWps === "number" && providedWps > 0 ? providedWps : DEFAULT_WORDS_PER_SECOND;
|
|
22
|
+
const spokenWords = countSpokenWords(text);
|
|
23
|
+
const totalSeconds = spokenWords / wps;
|
|
24
|
+
return {
|
|
25
|
+
spokenWords,
|
|
26
|
+
minutes: totalSeconds / 60,
|
|
27
|
+
seconds: Math.round(totalSeconds)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
estimateReadingTime,
|
|
33
|
+
estimateNarrationTime
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=chunk-6YJPI6VI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/timing/readingTime.ts"],"sourcesContent":["/**\n * Reading time estimation.\n *\n * Estimates how long it takes to read text silently vs. narrate it aloud.\n * Uses configurable words-per-minute rates.\n */\n\nimport { countSpokenWords, DEFAULT_WORDS_PER_SECOND } from './narrationTiming.js';\n\n/** Default silent reading rate (adults average ~200–250 WPM). */\nconst DEFAULT_READING_WPM = 200;\n\n/** Result of a reading-time estimate. */\nexport interface ReadingTimeEstimate {\n /** Number of words (plain word count, not spoken equivalents). */\n words: number;\n /** Estimated minutes (decimal). */\n minutes: number;\n /** Estimated seconds (rounded). */\n seconds: number;\n}\n\n/** Options for reading-time estimation. */\nexport interface ReadingTimeOptions {\n /** Words per minute for silent reading (default: 200). */\n wordsPerMinute?: number;\n}\n\n/**\n * Estimate silent reading time for a block of text.\n *\n * @param text - The text to estimate\n * @param options - Optional configuration\n * @returns Word count + time estimate\n */\nexport function estimateReadingTime(\n text: string,\n options?: ReadingTimeOptions,\n): ReadingTimeEstimate {\n const configuredWpm = options?.wordsPerMinute;\n const wpm =\n typeof configuredWpm === 'number' && configuredWpm > 0 ? configuredWpm : DEFAULT_READING_WPM;\n const words = text.split(/\\s+/).filter((w) => w.length > 0).length;\n const minutes = words / wpm;\n\n return {\n words,\n minutes,\n seconds: Math.round(minutes * 60),\n };\n}\n\n/** Result of a narration-time estimate. */\nexport interface NarrationTimeEstimate {\n /** Spoken word equivalents (numbers expanded, comma pauses added). */\n spokenWords: number;\n /** Estimated minutes (decimal). */\n minutes: number;\n /** Estimated seconds (rounded). */\n seconds: number;\n}\n\n/** Options for narration-time estimation. */\nexport interface NarrationTimeOptions {\n /** Words per second for narration (default: 2.5 ≈ 150 WPM). */\n wordsPerSecond?: number;\n}\n\n/**\n * Estimate narration (spoken) time for a block of text.\n *\n * Unlike silent reading, this accounts for numbers being spoken as\n * multiple words and pauses at commas.\n *\n * @param text - The text to estimate\n * @param options - Optional configuration\n * @returns Spoken word equivalents + time estimate\n */\nexport function estimateNarrationTime(\n text: string,\n options?: NarrationTimeOptions,\n): NarrationTimeEstimate {\n const providedWps = options?.wordsPerSecond;\n const wps =\n typeof providedWps === 'number' && providedWps > 0 ? providedWps : DEFAULT_WORDS_PER_SECOND;\n const spokenWords = countSpokenWords(text);\n const totalSeconds = spokenWords / wps;\n\n return {\n spokenWords,\n minutes: totalSeconds / 60,\n seconds: Math.round(totalSeconds),\n };\n}\n"],"mappings":";;;;;;AAUA,IAAM,sBAAsB;AAyBrB,SAAS,oBACd,MACA,SACqB;AACrB,QAAM,gBAAgB,SAAS;AAC/B,QAAM,MACJ,OAAO,kBAAkB,YAAY,gBAAgB,IAAI,gBAAgB;AAC3E,QAAM,QAAQ,KAAK,MAAM,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE;AAC5D,QAAM,UAAU,QAAQ;AAExB,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,SAAS,KAAK,MAAM,UAAU,EAAE;AAAA,EAClC;AACF;AA4BO,SAAS,sBACd,MACA,SACuB;AACvB,QAAM,cAAc,SAAS;AAC7B,QAAM,MACJ,OAAO,gBAAgB,YAAY,cAAc,IAAI,cAAc;AACrE,QAAM,cAAc,iBAAiB,IAAI;AACzC,QAAM,eAAe,cAAc;AAEnC,SAAO;AAAA,IACL;AAAA,IACA,SAAS,eAAe;AAAA,IACxB,SAAS,KAAK,MAAM,YAAY;AAAA,EAClC;AACF;","names":[]}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// src/random/SeededRandom.ts
|
|
2
|
+
var SeededRandom = class _SeededRandom {
|
|
3
|
+
constructor(seed) {
|
|
4
|
+
this.state = seed >>> 0;
|
|
5
|
+
if (this.state === 0) {
|
|
6
|
+
this.state = 3735928559;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** Random float in [0, 1). */
|
|
10
|
+
next() {
|
|
11
|
+
let t = this.state += 1831565813;
|
|
12
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
13
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
14
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
15
|
+
}
|
|
16
|
+
/** Random integer in [0, max). */
|
|
17
|
+
nextInt(max) {
|
|
18
|
+
return Math.floor(this.next() * max);
|
|
19
|
+
}
|
|
20
|
+
/** Random integer in [min, max). */
|
|
21
|
+
nextIntRange(min, max) {
|
|
22
|
+
return min + Math.floor(this.next() * (max - min));
|
|
23
|
+
}
|
|
24
|
+
/** Random boolean with given probability of `true` (default 0.5). */
|
|
25
|
+
nextBool(probability = 0.5) {
|
|
26
|
+
return this.next() < probability;
|
|
27
|
+
}
|
|
28
|
+
/** Pick a random element, or `undefined` if array is empty. */
|
|
29
|
+
pick(array) {
|
|
30
|
+
if (array.length === 0) return void 0;
|
|
31
|
+
return array[this.nextInt(array.length)];
|
|
32
|
+
}
|
|
33
|
+
/** Pick a random element; throws if empty. */
|
|
34
|
+
pickRequired(array) {
|
|
35
|
+
if (array.length === 0) {
|
|
36
|
+
throw new Error("Cannot pick from empty array");
|
|
37
|
+
}
|
|
38
|
+
return array[this.nextInt(array.length)];
|
|
39
|
+
}
|
|
40
|
+
/** Pick `count` unique random elements (fewer if array is smaller). */
|
|
41
|
+
pickMultiple(array, count) {
|
|
42
|
+
if (count >= array.length) {
|
|
43
|
+
return this.shuffle([...array]);
|
|
44
|
+
}
|
|
45
|
+
const result = [];
|
|
46
|
+
const available = [...array];
|
|
47
|
+
for (let i = 0; i < count && available.length > 0; i++) {
|
|
48
|
+
const index = this.nextInt(available.length);
|
|
49
|
+
result.push(available[index]);
|
|
50
|
+
available.splice(index, 1);
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/** Fisher-Yates shuffle **in place**. Returns the same array. */
|
|
55
|
+
shuffle(array) {
|
|
56
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
57
|
+
const j = this.nextInt(i + 1);
|
|
58
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
59
|
+
}
|
|
60
|
+
return array;
|
|
61
|
+
}
|
|
62
|
+
/** Return a **new** shuffled copy. */
|
|
63
|
+
shuffled(array) {
|
|
64
|
+
return this.shuffle([...array]);
|
|
65
|
+
}
|
|
66
|
+
/** Weighted random selection. Returns `undefined` if items is empty. */
|
|
67
|
+
pickWeighted(items) {
|
|
68
|
+
if (items.length === 0) return void 0;
|
|
69
|
+
const totalWeight = items.reduce((sum, { weight }) => sum + weight, 0);
|
|
70
|
+
if (totalWeight <= 0) return items[0]?.item;
|
|
71
|
+
let random = this.next() * totalWeight;
|
|
72
|
+
for (const { item, weight } of items) {
|
|
73
|
+
random -= weight;
|
|
74
|
+
if (random <= 0) return item;
|
|
75
|
+
}
|
|
76
|
+
return items[items.length - 1]?.item;
|
|
77
|
+
}
|
|
78
|
+
/** Current internal state (for debugging / serialization). */
|
|
79
|
+
getState() {
|
|
80
|
+
return this.state;
|
|
81
|
+
}
|
|
82
|
+
/** Create an independent sub-stream keyed by a modifier. */
|
|
83
|
+
derive(modifier) {
|
|
84
|
+
const hash = typeof modifier === "string" ? hashString(modifier) : modifier >>> 0;
|
|
85
|
+
return new _SeededRandom(this.state ^ hash);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
function hashString(str) {
|
|
89
|
+
let hash = 5381;
|
|
90
|
+
for (let i = 0; i < str.length; i++) {
|
|
91
|
+
hash = (hash << 5) + hash ^ str.charCodeAt(i);
|
|
92
|
+
}
|
|
93
|
+
return hash >>> 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export {
|
|
97
|
+
SeededRandom,
|
|
98
|
+
hashString
|
|
99
|
+
};
|
|
100
|
+
//# sourceMappingURL=chunk-FVTQSNR7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/random/SeededRandom.ts"],"sourcesContent":["/**\n * Seeded pseudo-random number generator (Mulberry32 algorithm).\n *\n * Provides deterministic, reproducible randomness from an integer seed.\n * Useful for consistent slideshow generation, shuffling, and selection.\n *\n * ```ts\n * const rng = new SeededRandom(42);\n * rng.next(); // 0.0–1.0\n * rng.nextInt(10); // 0–9\n * rng.pick(['a', 'b']); // 'a' or 'b'\n * ```\n */\nexport class SeededRandom {\n private state: number;\n\n constructor(seed: number) {\n this.state = seed >>> 0;\n if (this.state === 0) {\n this.state = 0xdeadbeef;\n }\n }\n\n /** Random float in [0, 1). */\n next(): number {\n let t = (this.state += 0x6d2b79f5);\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n }\n\n /** Random integer in [0, max). */\n nextInt(max: number): number {\n return Math.floor(this.next() * max);\n }\n\n /** Random integer in [min, max). */\n nextIntRange(min: number, max: number): number {\n return min + Math.floor(this.next() * (max - min));\n }\n\n /** Random boolean with given probability of `true` (default 0.5). */\n nextBool(probability: number = 0.5): boolean {\n return this.next() < probability;\n }\n\n /** Pick a random element, or `undefined` if array is empty. */\n pick<T>(array: T[]): T | undefined {\n if (array.length === 0) return undefined;\n return array[this.nextInt(array.length)];\n }\n\n /** Pick a random element; throws if empty. */\n pickRequired<T>(array: T[]): T {\n if (array.length === 0) {\n throw new Error('Cannot pick from empty array');\n }\n return array[this.nextInt(array.length)];\n }\n\n /** Pick `count` unique random elements (fewer if array is smaller). */\n pickMultiple<T>(array: T[], count: number): T[] {\n if (count >= array.length) {\n return this.shuffle([...array]);\n }\n const result: T[] = [];\n const available = [...array];\n for (let i = 0; i < count && available.length > 0; i++) {\n const index = this.nextInt(available.length);\n result.push(available[index]);\n available.splice(index, 1);\n }\n return result;\n }\n\n /** Fisher-Yates shuffle **in place**. Returns the same array. */\n shuffle<T>(array: T[]): T[] {\n for (let i = array.length - 1; i > 0; i--) {\n const j = this.nextInt(i + 1);\n [array[i], array[j]] = [array[j], array[i]];\n }\n return array;\n }\n\n /** Return a **new** shuffled copy. */\n shuffled<T>(array: readonly T[]): T[] {\n return this.shuffle([...array]);\n }\n\n /** Weighted random selection. Returns `undefined` if items is empty. */\n pickWeighted<T>(items: { item: T; weight: number }[]): T | undefined {\n if (items.length === 0) return undefined;\n\n const totalWeight = items.reduce((sum, { weight }) => sum + weight, 0);\n if (totalWeight <= 0) return items[0]?.item;\n\n let random = this.next() * totalWeight;\n for (const { item, weight } of items) {\n random -= weight;\n if (random <= 0) return item;\n }\n return items[items.length - 1]?.item;\n }\n\n /** Current internal state (for debugging / serialization). */\n getState(): number {\n return this.state;\n }\n\n /** Create an independent sub-stream keyed by a modifier. */\n derive(modifier: string | number): SeededRandom {\n const hash = typeof modifier === 'string' ? hashString(modifier) : modifier >>> 0;\n return new SeededRandom(this.state ^ hash);\n }\n}\n\n/**\n * Simple string → 32-bit hash (djb2 algorithm).\n * Useful for turning a document ID into a numeric seed.\n */\nexport function hashString(str: string): number {\n let hash = 5381;\n for (let i = 0; i < str.length; i++) {\n hash = ((hash << 5) + hash) ^ str.charCodeAt(i);\n }\n return hash >>> 0;\n}\n"],"mappings":";AAaO,IAAM,eAAN,MAAM,cAAa;AAAA,EAGxB,YAAY,MAAc;AACxB,SAAK,QAAQ,SAAS;AACtB,QAAI,KAAK,UAAU,GAAG;AACpB,WAAK,QAAQ;AAAA,IACf;AAAA,EACF;AAAA;AAAA,EAGA,OAAe;AACb,QAAI,IAAK,KAAK,SAAS;AACvB,QAAI,KAAK,KAAK,IAAK,MAAM,IAA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