@memberjunction/core-actions 2.47.0 → 2.49.0
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/custom/business-days-calculator.action.d.ts +65 -0
- package/dist/custom/business-days-calculator.action.d.ts.map +1 -0
- package/dist/custom/business-days-calculator.action.js +299 -0
- package/dist/custom/business-days-calculator.action.js.map +1 -0
- package/dist/custom/calculate-expression.action.d.ts +56 -0
- package/dist/custom/calculate-expression.action.d.ts.map +1 -0
- package/dist/custom/calculate-expression.action.js +226 -0
- package/dist/custom/calculate-expression.action.js.map +1 -0
- package/dist/custom/census-data-lookup.action.d.ts +54 -0
- package/dist/custom/census-data-lookup.action.d.ts.map +1 -0
- package/dist/custom/census-data-lookup.action.js +378 -0
- package/dist/custom/census-data-lookup.action.js.map +1 -0
- package/dist/custom/color-converter.action.d.ts +59 -0
- package/dist/custom/color-converter.action.d.ts.map +1 -0
- package/dist/custom/color-converter.action.js +295 -0
- package/dist/custom/color-converter.action.js.map +1 -0
- package/dist/custom/external-change-detection.action.js +1 -1
- package/dist/custom/external-change-detection.action.js.map +1 -1
- package/dist/custom/get-stock-price.action.d.ts +48 -0
- package/dist/custom/get-stock-price.action.d.ts.map +1 -0
- package/dist/custom/get-stock-price.action.js +153 -0
- package/dist/custom/get-stock-price.action.js.map +1 -0
- package/dist/custom/get-weather.action.d.ts +52 -0
- package/dist/custom/get-weather.action.d.ts.map +1 -0
- package/dist/custom/get-weather.action.js +172 -0
- package/dist/custom/get-weather.action.js.map +1 -0
- package/dist/custom/ip-geolocation.action.d.ts +43 -0
- package/dist/custom/ip-geolocation.action.d.ts.map +1 -0
- package/dist/custom/ip-geolocation.action.js +259 -0
- package/dist/custom/ip-geolocation.action.js.map +1 -0
- package/dist/custom/send-single-message.action.js +1 -1
- package/dist/custom/text-analyzer.action.d.ts +53 -0
- package/dist/custom/text-analyzer.action.d.ts.map +1 -0
- package/dist/custom/text-analyzer.action.js +218 -0
- package/dist/custom/text-analyzer.action.js.map +1 -0
- package/dist/custom/unit-converter.action.d.ts +63 -0
- package/dist/custom/unit-converter.action.d.ts.map +1 -0
- package/dist/custom/unit-converter.action.js +357 -0
- package/dist/custom/unit-converter.action.js.map +1 -0
- package/dist/custom/vectorize-entity.action.d.ts +45 -9
- package/dist/custom/vectorize-entity.action.d.ts.map +1 -1
- package/dist/custom/vectorize-entity.action.js +46 -10
- package/dist/custom/vectorize-entity.action.js.map +1 -1
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/package.json +10 -9
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.LoadTextAnalyzerAction = exports.TextAnalyzerAction = void 0;
|
|
10
|
+
const actions_1 = require("@memberjunction/actions");
|
|
11
|
+
const global_1 = require("@memberjunction/global");
|
|
12
|
+
/**
|
|
13
|
+
* Action that analyzes text content and provides various statistics including
|
|
14
|
+
* word count, character count, readability scores, and common word analysis.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Analyze a simple text
|
|
19
|
+
* await runAction({
|
|
20
|
+
* ActionName: 'Text Analyzer',
|
|
21
|
+
* Params: [{
|
|
22
|
+
* Name: 'Text',
|
|
23
|
+
* Value: 'The quick brown fox jumps over the lazy dog.'
|
|
24
|
+
* }]
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Analyze with custom options
|
|
28
|
+
* await runAction({
|
|
29
|
+
* ActionName: 'Text Analyzer',
|
|
30
|
+
* Params: [{
|
|
31
|
+
* Name: 'Text',
|
|
32
|
+
* Value: 'Your text here...'
|
|
33
|
+
* }, {
|
|
34
|
+
* Name: 'IncludeWordFrequency',
|
|
35
|
+
* Value: true
|
|
36
|
+
* }]
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
let TextAnalyzerAction = class TextAnalyzerAction extends actions_1.BaseAction {
|
|
41
|
+
/**
|
|
42
|
+
* Executes the text analysis action
|
|
43
|
+
*
|
|
44
|
+
* @param params - The action parameters containing:
|
|
45
|
+
* - Text: The text content to analyze
|
|
46
|
+
* - IncludeWordFrequency: Optional boolean to include word frequency analysis
|
|
47
|
+
*
|
|
48
|
+
* @returns A promise resolving to an ActionResultSimple with text analysis data
|
|
49
|
+
*/
|
|
50
|
+
async InternalRunAction(params) {
|
|
51
|
+
try {
|
|
52
|
+
const textParam = params.Params.find(p => p.Name === 'Text');
|
|
53
|
+
const includeFreqParam = params.Params.find(p => p.Name === 'IncludeWordFrequency');
|
|
54
|
+
if (!textParam || !textParam.Value) {
|
|
55
|
+
return {
|
|
56
|
+
Success: false,
|
|
57
|
+
Message: "Text parameter is required",
|
|
58
|
+
ResultCode: "MISSING_PARAMETERS"
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const text = textParam.Value;
|
|
62
|
+
const includeFrequency = includeFreqParam?.Value === true || includeFreqParam?.Value === 'true';
|
|
63
|
+
// Basic counts
|
|
64
|
+
const charCount = text.length;
|
|
65
|
+
const charCountNoSpaces = text.replace(/\s/g, '').length;
|
|
66
|
+
const words = this.extractWords(text);
|
|
67
|
+
const wordCount = words.length;
|
|
68
|
+
const sentences = this.extractSentences(text);
|
|
69
|
+
const sentenceCount = sentences.length;
|
|
70
|
+
const paragraphs = text.split(/\n\n+/).filter(p => p.trim().length > 0);
|
|
71
|
+
const paragraphCount = paragraphs.length;
|
|
72
|
+
const lines = text.split('\n').filter(l => l.trim().length > 0);
|
|
73
|
+
const lineCount = lines.length;
|
|
74
|
+
// Calculate averages
|
|
75
|
+
const avgWordLength = wordCount > 0 ?
|
|
76
|
+
words.reduce((sum, word) => sum + word.length, 0) / wordCount : 0;
|
|
77
|
+
const avgWordsPerSentence = sentenceCount > 0 ? wordCount / sentenceCount : 0;
|
|
78
|
+
const avgWordsPerParagraph = paragraphCount > 0 ? wordCount / paragraphCount : 0;
|
|
79
|
+
// Readability scores
|
|
80
|
+
const fleschScore = this.calculateFleschReadingEase(avgWordsPerSentence, avgWordLength);
|
|
81
|
+
const readingLevel = this.getReadingLevel(fleschScore);
|
|
82
|
+
// Character analysis
|
|
83
|
+
const letterCount = text.match(/[a-zA-Z]/g)?.length || 0;
|
|
84
|
+
const digitCount = text.match(/\d/g)?.length || 0;
|
|
85
|
+
const punctuationCount = text.match(/[.,;:!?'"()\-]/g)?.length || 0;
|
|
86
|
+
const whitespaceCount = text.match(/\s/g)?.length || 0;
|
|
87
|
+
// Build result
|
|
88
|
+
const analysisData = {
|
|
89
|
+
summary: {
|
|
90
|
+
characters: charCount,
|
|
91
|
+
charactersNoSpaces: charCountNoSpaces,
|
|
92
|
+
words: wordCount,
|
|
93
|
+
sentences: sentenceCount,
|
|
94
|
+
paragraphs: paragraphCount,
|
|
95
|
+
lines: lineCount
|
|
96
|
+
},
|
|
97
|
+
averages: {
|
|
98
|
+
wordLength: parseFloat(avgWordLength.toFixed(2)),
|
|
99
|
+
wordsPerSentence: parseFloat(avgWordsPerSentence.toFixed(2)),
|
|
100
|
+
wordsPerParagraph: parseFloat(avgWordsPerParagraph.toFixed(2))
|
|
101
|
+
},
|
|
102
|
+
characterBreakdown: {
|
|
103
|
+
letters: letterCount,
|
|
104
|
+
digits: digitCount,
|
|
105
|
+
punctuation: punctuationCount,
|
|
106
|
+
whitespace: whitespaceCount,
|
|
107
|
+
other: charCount - letterCount - digitCount - punctuationCount - whitespaceCount
|
|
108
|
+
},
|
|
109
|
+
readability: {
|
|
110
|
+
fleschReadingEase: parseFloat(fleschScore.toFixed(2)),
|
|
111
|
+
readingLevel: readingLevel,
|
|
112
|
+
estimatedReadingTimeMinutes: parseFloat((wordCount / 200).toFixed(2))
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
// Add word frequency if requested
|
|
116
|
+
if (includeFrequency && words.length > 0) {
|
|
117
|
+
const frequency = this.calculateWordFrequency(words);
|
|
118
|
+
analysisData.wordFrequency = {
|
|
119
|
+
uniqueWords: frequency.size,
|
|
120
|
+
topWords: Array.from(frequency.entries())
|
|
121
|
+
.sort((a, b) => b[1] - a[1])
|
|
122
|
+
.slice(0, 20)
|
|
123
|
+
.map(([word, count]) => ({ word, count, percentage: parseFloat((count / wordCount * 100).toFixed(2)) }))
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// Language detection hints
|
|
127
|
+
const hasNumbers = digitCount > 0;
|
|
128
|
+
const hasUppercase = text !== text.toLowerCase();
|
|
129
|
+
const hasPunctuation = punctuationCount > 0;
|
|
130
|
+
analysisData.textFeatures = {
|
|
131
|
+
hasNumbers,
|
|
132
|
+
hasUppercase,
|
|
133
|
+
hasPunctuation,
|
|
134
|
+
appearsToBe: this.guessTextType(avgWordsPerSentence, hasPunctuation, paragraphCount)
|
|
135
|
+
};
|
|
136
|
+
return {
|
|
137
|
+
Success: true,
|
|
138
|
+
ResultCode: "SUCCESS",
|
|
139
|
+
Message: JSON.stringify(analysisData, null, 2)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
return {
|
|
144
|
+
Success: false,
|
|
145
|
+
Message: `Failed to analyze text: ${error instanceof Error ? error.message : String(error)}`,
|
|
146
|
+
ResultCode: "FAILED"
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
extractWords(text) {
|
|
151
|
+
// Extract words (sequences of letters and digits)
|
|
152
|
+
return text.match(/\b[\w']+\b/g)?.map(w => w.toLowerCase()) || [];
|
|
153
|
+
}
|
|
154
|
+
extractSentences(text) {
|
|
155
|
+
// Simple sentence extraction (ends with . ! ? followed by space or end)
|
|
156
|
+
return text.match(/[^.!?]+[.!?]+/g)?.map(s => s.trim()).filter(s => s.length > 0) || [text];
|
|
157
|
+
}
|
|
158
|
+
calculateWordFrequency(words) {
|
|
159
|
+
const frequency = new Map();
|
|
160
|
+
const commonWords = new Set(['the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have',
|
|
161
|
+
'i', 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at', 'this',
|
|
162
|
+
'but', 'his', 'by', 'from', 'is', 'was', 'are', 'been', 'has', 'had', 'were',
|
|
163
|
+
'said', 'did', 'get', 'may', 'will', 'would', 'could', 'can', 'if', 'or', 'an']);
|
|
164
|
+
for (const word of words) {
|
|
165
|
+
if (word.length > 2 && !commonWords.has(word)) {
|
|
166
|
+
frequency.set(word, (frequency.get(word) || 0) + 1);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return frequency;
|
|
170
|
+
}
|
|
171
|
+
calculateFleschReadingEase(avgWordsPerSentence, avgWordLength) {
|
|
172
|
+
// Simplified Flesch Reading Ease formula
|
|
173
|
+
// Using average word length as proxy for syllables (divide by 3 for rough estimate)
|
|
174
|
+
const avgSyllablesPerWord = avgWordLength / 3;
|
|
175
|
+
const score = 206.835 - 1.015 * avgWordsPerSentence - 84.6 * avgSyllablesPerWord;
|
|
176
|
+
return Math.max(0, Math.min(100, score));
|
|
177
|
+
}
|
|
178
|
+
getReadingLevel(fleschScore) {
|
|
179
|
+
if (fleschScore >= 90)
|
|
180
|
+
return "Very Easy (5th grade)";
|
|
181
|
+
if (fleschScore >= 80)
|
|
182
|
+
return "Easy (6th grade)";
|
|
183
|
+
if (fleschScore >= 70)
|
|
184
|
+
return "Fairly Easy (7th grade)";
|
|
185
|
+
if (fleschScore >= 60)
|
|
186
|
+
return "Plain English (8th-9th grade)";
|
|
187
|
+
if (fleschScore >= 50)
|
|
188
|
+
return "Fairly Difficult (10th-12th grade)";
|
|
189
|
+
if (fleschScore >= 30)
|
|
190
|
+
return "Difficult (College)";
|
|
191
|
+
if (fleschScore >= 10)
|
|
192
|
+
return "Very Difficult (Graduate)";
|
|
193
|
+
return "Extremely Difficult (Professional)";
|
|
194
|
+
}
|
|
195
|
+
guessTextType(avgWordsPerSentence, hasPunctuation, paragraphCount) {
|
|
196
|
+
if (!hasPunctuation)
|
|
197
|
+
return "List or notes";
|
|
198
|
+
if (avgWordsPerSentence < 10)
|
|
199
|
+
return "Simple text or dialogue";
|
|
200
|
+
if (avgWordsPerSentence > 25)
|
|
201
|
+
return "Academic or technical writing";
|
|
202
|
+
if (paragraphCount > 3)
|
|
203
|
+
return "Article or essay";
|
|
204
|
+
return "General text";
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
exports.TextAnalyzerAction = TextAnalyzerAction;
|
|
208
|
+
exports.TextAnalyzerAction = TextAnalyzerAction = __decorate([
|
|
209
|
+
(0, global_1.RegisterClass)(actions_1.BaseAction, "__TextAnalyzer")
|
|
210
|
+
], TextAnalyzerAction);
|
|
211
|
+
/**
|
|
212
|
+
* Loader function to ensure the TextAnalyzerAction class is included in the bundle.
|
|
213
|
+
*/
|
|
214
|
+
function LoadTextAnalyzerAction() {
|
|
215
|
+
// this function is a stub that is used to force the bundler to include the above class in the final bundle and not tree shake them out
|
|
216
|
+
}
|
|
217
|
+
exports.LoadTextAnalyzerAction = LoadTextAnalyzerAction;
|
|
218
|
+
//# sourceMappingURL=text-analyzer.action.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-analyzer.action.js","sourceRoot":"","sources":["../../src/custom/text-analyzer.action.ts"],"names":[],"mappings":";;;;;;;;;A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mBAAmB,GAAG,EAAE;YAAE,OAAO,yBAAyB,CAAC;QAC/D,IAAI,mBAAmB,GAAG,EAAE;YAAE,OAAO,+BAA+B,CAAC;QACrE,IAAI,cAAc,GAAG,CAAC;YAAE,OAAO,kBAAkB,CAAC;QAClD,OAAO,cAAc,CAAC;IAC1B,CAAC;CACJ,CAAA;AA7KY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,sBAAa,EAAC,oBAAU,EAAE,gBAAgB,CAAC;GAC/B,kBAAkB,CA6K9B;AAED;;GAEG;AACH,SAAgB,sBAAsB;IAClC,uIAAuI;AAC3I,CAAC;AAFD,wDAEC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ActionResultSimple, RunActionParams } from "@memberjunction/actions-base";
|
|
2
|
+
import { BaseAction } from "@memberjunction/actions";
|
|
3
|
+
/**
|
|
4
|
+
* Action that converts values between different units of measurement.
|
|
5
|
+
* Supports length, weight, temperature, volume, area, speed, and time conversions.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Convert miles to kilometers
|
|
10
|
+
* await runAction({
|
|
11
|
+
* ActionName: 'Unit Converter',
|
|
12
|
+
* Params: [{
|
|
13
|
+
* Name: 'Value',
|
|
14
|
+
* Value: 5
|
|
15
|
+
* }, {
|
|
16
|
+
* Name: 'FromUnit',
|
|
17
|
+
* Value: 'miles'
|
|
18
|
+
* }, {
|
|
19
|
+
* Name: 'ToUnit',
|
|
20
|
+
* Value: 'kilometers'
|
|
21
|
+
* }]
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Convert temperature
|
|
25
|
+
* await runAction({
|
|
26
|
+
* ActionName: 'Unit Converter',
|
|
27
|
+
* Params: [{
|
|
28
|
+
* Name: 'Value',
|
|
29
|
+
* Value: 32
|
|
30
|
+
* }, {
|
|
31
|
+
* Name: 'FromUnit',
|
|
32
|
+
* Value: 'fahrenheit'
|
|
33
|
+
* }, {
|
|
34
|
+
* Name: 'ToUnit',
|
|
35
|
+
* Value: 'celsius'
|
|
36
|
+
* }]
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare class UnitConverterAction extends BaseAction {
|
|
41
|
+
private conversions;
|
|
42
|
+
/**
|
|
43
|
+
* Executes the unit conversion action
|
|
44
|
+
*
|
|
45
|
+
* @param params - The action parameters containing:
|
|
46
|
+
* - Value: The numeric value to convert
|
|
47
|
+
* - FromUnit: The unit to convert from
|
|
48
|
+
* - ToUnit: The unit to convert to
|
|
49
|
+
*
|
|
50
|
+
* @returns A promise resolving to an ActionResultSimple with conversion data
|
|
51
|
+
*/
|
|
52
|
+
protected InternalRunAction(params: RunActionParams): Promise<ActionResultSimple>;
|
|
53
|
+
private isTemperature;
|
|
54
|
+
private convertTemperature;
|
|
55
|
+
private getTemperatureFormula;
|
|
56
|
+
private formatResult;
|
|
57
|
+
private getAlternativeConversions;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Loader function to ensure the UnitConverterAction class is included in the bundle.
|
|
61
|
+
*/
|
|
62
|
+
export declare function LoadUnitConverterAction(): void;
|
|
63
|
+
//# sourceMappingURL=unit-converter.action.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unit-converter.action.d.ts","sourceRoot":"","sources":["../../src/custom/unit-converter.action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAe,MAAM,8BAA8B,CAAC;AAChG,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,qBACa,mBAAoB,SAAQ,UAAU;IAC/C,OAAO,CAAC,WAAW,CAsEjB;IAEF;;;;;;;;;OASG;cACa,iBAAiB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAuIvF,OAAO,CAAC,aAAa;IAKrB,OAAO,CAAC,kBAAkB;IA2C1B,OAAO,CAAC,qBAAqB;IAY7B,OAAO,CAAC,YAAY;IAapB,OAAO,CAAC,yBAAyB;CAyBpC;AAED;;GAEG;AACH,wBAAgB,uBAAuB,SAEtC"}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.LoadUnitConverterAction = exports.UnitConverterAction = void 0;
|
|
10
|
+
const actions_1 = require("@memberjunction/actions");
|
|
11
|
+
const global_1 = require("@memberjunction/global");
|
|
12
|
+
/**
|
|
13
|
+
* Action that converts values between different units of measurement.
|
|
14
|
+
* Supports length, weight, temperature, volume, area, speed, and time conversions.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Convert miles to kilometers
|
|
19
|
+
* await runAction({
|
|
20
|
+
* ActionName: 'Unit Converter',
|
|
21
|
+
* Params: [{
|
|
22
|
+
* Name: 'Value',
|
|
23
|
+
* Value: 5
|
|
24
|
+
* }, {
|
|
25
|
+
* Name: 'FromUnit',
|
|
26
|
+
* Value: 'miles'
|
|
27
|
+
* }, {
|
|
28
|
+
* Name: 'ToUnit',
|
|
29
|
+
* Value: 'kilometers'
|
|
30
|
+
* }]
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // Convert temperature
|
|
34
|
+
* await runAction({
|
|
35
|
+
* ActionName: 'Unit Converter',
|
|
36
|
+
* Params: [{
|
|
37
|
+
* Name: 'Value',
|
|
38
|
+
* Value: 32
|
|
39
|
+
* }, {
|
|
40
|
+
* Name: 'FromUnit',
|
|
41
|
+
* Value: 'fahrenheit'
|
|
42
|
+
* }, {
|
|
43
|
+
* Name: 'ToUnit',
|
|
44
|
+
* Value: 'celsius'
|
|
45
|
+
* }]
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
let UnitConverterAction = class UnitConverterAction extends actions_1.BaseAction {
|
|
50
|
+
conversions = {
|
|
51
|
+
// Length conversions (base: meters)
|
|
52
|
+
length: {
|
|
53
|
+
meter: 1, meters: 1, m: 1,
|
|
54
|
+
kilometer: 0.001, kilometers: 0.001, km: 0.001,
|
|
55
|
+
centimeter: 100, centimeters: 100, cm: 100,
|
|
56
|
+
millimeter: 1000, millimeters: 1000, mm: 1000,
|
|
57
|
+
mile: 0.000621371, miles: 0.000621371, mi: 0.000621371,
|
|
58
|
+
yard: 1.09361, yards: 1.09361, yd: 1.09361,
|
|
59
|
+
foot: 3.28084, feet: 3.28084, ft: 3.28084,
|
|
60
|
+
inch: 39.3701, inches: 39.3701, in: 39.3701,
|
|
61
|
+
nautical_mile: 0.000539957, nautical_miles: 0.000539957, nmi: 0.000539957
|
|
62
|
+
},
|
|
63
|
+
// Weight conversions (base: grams)
|
|
64
|
+
weight: {
|
|
65
|
+
gram: 1, grams: 1, g: 1,
|
|
66
|
+
kilogram: 0.001, kilograms: 0.001, kg: 0.001,
|
|
67
|
+
milligram: 1000, milligrams: 1000, mg: 1000,
|
|
68
|
+
pound: 0.00220462, pounds: 0.00220462, lb: 0.00220462, lbs: 0.00220462,
|
|
69
|
+
ounce: 0.035274, ounces: 0.035274, oz: 0.035274,
|
|
70
|
+
ton: 0.000001, tons: 0.000001, t: 0.000001,
|
|
71
|
+
metric_ton: 0.000001, metric_tons: 0.000001,
|
|
72
|
+
stone: 0.000157473, stones: 0.000157473
|
|
73
|
+
},
|
|
74
|
+
// Volume conversions (base: liters)
|
|
75
|
+
volume: {
|
|
76
|
+
liter: 1, liters: 1, l: 1,
|
|
77
|
+
milliliter: 1000, milliliters: 1000, ml: 1000,
|
|
78
|
+
gallon: 0.264172, gallons: 0.264172, gal: 0.264172,
|
|
79
|
+
quart: 1.05669, quarts: 1.05669, qt: 1.05669,
|
|
80
|
+
pint: 2.11338, pints: 2.11338, pt: 2.11338,
|
|
81
|
+
cup: 4.22675, cups: 4.22675,
|
|
82
|
+
fluid_ounce: 33.814, fluid_ounces: 33.814, fl_oz: 33.814,
|
|
83
|
+
tablespoon: 67.628, tablespoons: 67.628, tbsp: 67.628,
|
|
84
|
+
teaspoon: 202.884, teaspoons: 202.884, tsp: 202.884,
|
|
85
|
+
cubic_meter: 0.001, cubic_meters: 0.001, m3: 0.001,
|
|
86
|
+
cubic_foot: 0.0353147, cubic_feet: 0.0353147, ft3: 0.0353147
|
|
87
|
+
},
|
|
88
|
+
// Area conversions (base: square meters)
|
|
89
|
+
area: {
|
|
90
|
+
square_meter: 1, square_meters: 1, m2: 1, sqm: 1,
|
|
91
|
+
square_kilometer: 0.000001, square_kilometers: 0.000001, km2: 0.000001,
|
|
92
|
+
square_mile: 0.000000386102, square_miles: 0.000000386102, mi2: 0.000000386102,
|
|
93
|
+
square_yard: 1.19599, square_yards: 1.19599, yd2: 1.19599,
|
|
94
|
+
square_foot: 10.7639, square_feet: 10.7639, ft2: 10.7639, sqft: 10.7639,
|
|
95
|
+
square_inch: 1550, square_inches: 1550, in2: 1550,
|
|
96
|
+
hectare: 0.0001, hectares: 0.0001, ha: 0.0001,
|
|
97
|
+
acre: 0.000247105, acres: 0.000247105, ac: 0.000247105
|
|
98
|
+
},
|
|
99
|
+
// Speed conversions (base: meters per second)
|
|
100
|
+
speed: {
|
|
101
|
+
meters_per_second: 1, mps: 1, 'm/s': 1,
|
|
102
|
+
kilometers_per_hour: 3.6, kph: 3.6, kmh: 3.6, 'km/h': 3.6,
|
|
103
|
+
miles_per_hour: 2.23694, mph: 2.23694, 'mi/h': 2.23694,
|
|
104
|
+
feet_per_second: 3.28084, fps: 3.28084, 'ft/s': 3.28084,
|
|
105
|
+
knots: 1.94384, knot: 1.94384, kn: 1.94384
|
|
106
|
+
},
|
|
107
|
+
// Time conversions (base: seconds)
|
|
108
|
+
time: {
|
|
109
|
+
second: 1, seconds: 1, s: 1, sec: 1,
|
|
110
|
+
millisecond: 1000, milliseconds: 1000, ms: 1000,
|
|
111
|
+
microsecond: 1000000, microseconds: 1000000, μs: 1000000,
|
|
112
|
+
nanosecond: 1000000000, nanoseconds: 1000000000, ns: 1000000000,
|
|
113
|
+
minute: 0.0166667, minutes: 0.0166667, min: 0.0166667,
|
|
114
|
+
hour: 0.000277778, hours: 0.000277778, h: 0.000277778, hr: 0.000277778,
|
|
115
|
+
day: 0.0000115741, days: 0.0000115741, d: 0.0000115741,
|
|
116
|
+
week: 0.00000165344, weeks: 0.00000165344, wk: 0.00000165344,
|
|
117
|
+
month: 0.00000038052, months: 0.00000038052, mo: 0.00000038052,
|
|
118
|
+
year: 0.0000000316881, years: 0.0000000316881, yr: 0.0000000316881
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Executes the unit conversion action
|
|
123
|
+
*
|
|
124
|
+
* @param params - The action parameters containing:
|
|
125
|
+
* - Value: The numeric value to convert
|
|
126
|
+
* - FromUnit: The unit to convert from
|
|
127
|
+
* - ToUnit: The unit to convert to
|
|
128
|
+
*
|
|
129
|
+
* @returns A promise resolving to an ActionResultSimple with conversion data
|
|
130
|
+
*/
|
|
131
|
+
async InternalRunAction(params) {
|
|
132
|
+
try {
|
|
133
|
+
const valueParam = params.Params.find(p => p.Name === 'Value');
|
|
134
|
+
const fromUnitParam = params.Params.find(p => p.Name === 'FromUnit');
|
|
135
|
+
const toUnitParam = params.Params.find(p => p.Name === 'ToUnit');
|
|
136
|
+
if (!valueParam || valueParam.Value === undefined || valueParam.Value === null) {
|
|
137
|
+
return {
|
|
138
|
+
Success: false,
|
|
139
|
+
Message: "Value parameter is required",
|
|
140
|
+
ResultCode: "MISSING_PARAMETERS"
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (!fromUnitParam || !fromUnitParam.Value) {
|
|
144
|
+
return {
|
|
145
|
+
Success: false,
|
|
146
|
+
Message: "FromUnit parameter is required",
|
|
147
|
+
ResultCode: "MISSING_PARAMETERS"
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
if (!toUnitParam || !toUnitParam.Value) {
|
|
151
|
+
return {
|
|
152
|
+
Success: false,
|
|
153
|
+
Message: "ToUnit parameter is required",
|
|
154
|
+
ResultCode: "MISSING_PARAMETERS"
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const value = parseFloat(valueParam.Value.toString());
|
|
158
|
+
if (isNaN(value)) {
|
|
159
|
+
return {
|
|
160
|
+
Success: false,
|
|
161
|
+
Message: "Value must be a valid number",
|
|
162
|
+
ResultCode: "INVALID_VALUE"
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
const fromUnit = fromUnitParam.Value.toLowerCase().replace(/\s+/g, '_');
|
|
166
|
+
const toUnit = toUnitParam.Value.toLowerCase().replace(/\s+/g, '_');
|
|
167
|
+
// Handle temperature separately as it's not a simple multiplication
|
|
168
|
+
if (this.isTemperature(fromUnit) || this.isTemperature(toUnit)) {
|
|
169
|
+
const result = this.convertTemperature(value, fromUnit, toUnit);
|
|
170
|
+
if (result === null) {
|
|
171
|
+
return {
|
|
172
|
+
Success: false,
|
|
173
|
+
Message: `Cannot convert between ${fromUnit} and ${toUnit}`,
|
|
174
|
+
ResultCode: "INCOMPATIBLE_UNITS"
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
const conversionData = {
|
|
178
|
+
original: {
|
|
179
|
+
value: value,
|
|
180
|
+
unit: fromUnitParam.Value
|
|
181
|
+
},
|
|
182
|
+
converted: {
|
|
183
|
+
value: result,
|
|
184
|
+
unit: toUnitParam.Value,
|
|
185
|
+
formatted: `${result.toFixed(2)} ${toUnitParam.Value}`
|
|
186
|
+
},
|
|
187
|
+
category: 'temperature',
|
|
188
|
+
formula: this.getTemperatureFormula(fromUnit, toUnit)
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
Success: true,
|
|
192
|
+
ResultCode: "SUCCESS",
|
|
193
|
+
Message: JSON.stringify(conversionData, null, 2)
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// Find the category for both units
|
|
197
|
+
let fromCategory = null;
|
|
198
|
+
let toCategory = null;
|
|
199
|
+
for (const [category, units] of Object.entries(this.conversions)) {
|
|
200
|
+
if (units[fromUnit])
|
|
201
|
+
fromCategory = category;
|
|
202
|
+
if (units[toUnit])
|
|
203
|
+
toCategory = category;
|
|
204
|
+
}
|
|
205
|
+
if (!fromCategory || !toCategory) {
|
|
206
|
+
return {
|
|
207
|
+
Success: false,
|
|
208
|
+
Message: `Unknown unit(s): ${!fromCategory ? fromUnit : ''} ${!toCategory ? toUnit : ''}`,
|
|
209
|
+
ResultCode: "UNKNOWN_UNIT"
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if (fromCategory !== toCategory) {
|
|
213
|
+
return {
|
|
214
|
+
Success: false,
|
|
215
|
+
Message: `Cannot convert between ${fromCategory} and ${toCategory} units`,
|
|
216
|
+
ResultCode: "INCOMPATIBLE_UNITS"
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
// Perform conversion
|
|
220
|
+
const fromFactor = this.conversions[fromCategory][fromUnit];
|
|
221
|
+
const toFactor = this.conversions[fromCategory][toUnit];
|
|
222
|
+
const baseValue = value / fromFactor;
|
|
223
|
+
const result = baseValue * toFactor;
|
|
224
|
+
const conversionData = {
|
|
225
|
+
original: {
|
|
226
|
+
value: value,
|
|
227
|
+
unit: fromUnitParam.Value
|
|
228
|
+
},
|
|
229
|
+
converted: {
|
|
230
|
+
value: result,
|
|
231
|
+
unit: toUnitParam.Value,
|
|
232
|
+
formatted: this.formatResult(result, toUnitParam.Value)
|
|
233
|
+
},
|
|
234
|
+
category: fromCategory,
|
|
235
|
+
conversionFactor: toFactor / fromFactor,
|
|
236
|
+
alternativeUnits: this.getAlternativeConversions(baseValue, fromCategory, toUnit)
|
|
237
|
+
};
|
|
238
|
+
return {
|
|
239
|
+
Success: true,
|
|
240
|
+
ResultCode: "SUCCESS",
|
|
241
|
+
Message: JSON.stringify(conversionData, null, 2)
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
return {
|
|
246
|
+
Success: false,
|
|
247
|
+
Message: `Failed to convert units: ${error instanceof Error ? error.message : String(error)}`,
|
|
248
|
+
ResultCode: "FAILED"
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
isTemperature(unit) {
|
|
253
|
+
const tempUnits = ['celsius', 'c', 'fahrenheit', 'f', 'kelvin', 'k'];
|
|
254
|
+
return tempUnits.includes(unit);
|
|
255
|
+
}
|
|
256
|
+
convertTemperature(value, from, to) {
|
|
257
|
+
// Normalize unit names
|
|
258
|
+
const normalizeTemp = (unit) => {
|
|
259
|
+
if (unit === 'c')
|
|
260
|
+
return 'celsius';
|
|
261
|
+
if (unit === 'f')
|
|
262
|
+
return 'fahrenheit';
|
|
263
|
+
if (unit === 'k')
|
|
264
|
+
return 'kelvin';
|
|
265
|
+
return unit;
|
|
266
|
+
};
|
|
267
|
+
from = normalizeTemp(from);
|
|
268
|
+
to = normalizeTemp(to);
|
|
269
|
+
if (from === to)
|
|
270
|
+
return value;
|
|
271
|
+
// Convert to Celsius first
|
|
272
|
+
let celsius;
|
|
273
|
+
switch (from) {
|
|
274
|
+
case 'celsius':
|
|
275
|
+
celsius = value;
|
|
276
|
+
break;
|
|
277
|
+
case 'fahrenheit':
|
|
278
|
+
celsius = (value - 32) * 5 / 9;
|
|
279
|
+
break;
|
|
280
|
+
case 'kelvin':
|
|
281
|
+
celsius = value - 273.15;
|
|
282
|
+
break;
|
|
283
|
+
default:
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
// Convert from Celsius to target
|
|
287
|
+
switch (to) {
|
|
288
|
+
case 'celsius':
|
|
289
|
+
return celsius;
|
|
290
|
+
case 'fahrenheit':
|
|
291
|
+
return celsius * 9 / 5 + 32;
|
|
292
|
+
case 'kelvin':
|
|
293
|
+
return celsius + 273.15;
|
|
294
|
+
default:
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
getTemperatureFormula(from, to) {
|
|
299
|
+
const formulas = {
|
|
300
|
+
'celsius_fahrenheit': '°F = °C × 9/5 + 32',
|
|
301
|
+
'fahrenheit_celsius': '°C = (°F - 32) × 5/9',
|
|
302
|
+
'celsius_kelvin': 'K = °C + 273.15',
|
|
303
|
+
'kelvin_celsius': '°C = K - 273.15',
|
|
304
|
+
'fahrenheit_kelvin': 'K = (°F - 32) × 5/9 + 273.15',
|
|
305
|
+
'kelvin_fahrenheit': '°F = (K - 273.15) × 9/5 + 32'
|
|
306
|
+
};
|
|
307
|
+
return formulas[`${from}_${to}`] || '';
|
|
308
|
+
}
|
|
309
|
+
formatResult(value, unit) {
|
|
310
|
+
// Format based on the magnitude of the result
|
|
311
|
+
if (Math.abs(value) < 0.01 || Math.abs(value) > 10000) {
|
|
312
|
+
return `${value.toExponential(4)} ${unit}`;
|
|
313
|
+
}
|
|
314
|
+
else if (Number.isInteger(value)) {
|
|
315
|
+
return `${value} ${unit}`;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
// Determine decimal places based on value
|
|
319
|
+
const decimals = Math.abs(value) < 1 ? 4 : 2;
|
|
320
|
+
return `${value.toFixed(decimals)} ${unit}`;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
getAlternativeConversions(baseValue, category, excludeUnit) {
|
|
324
|
+
const alternatives = [];
|
|
325
|
+
const commonUnits = {
|
|
326
|
+
length: ['meters', 'kilometers', 'miles', 'feet'],
|
|
327
|
+
weight: ['grams', 'kilograms', 'pounds', 'ounces'],
|
|
328
|
+
volume: ['liters', 'milliliters', 'gallons', 'cups'],
|
|
329
|
+
area: ['square_meters', 'square_feet', 'acres', 'hectares'],
|
|
330
|
+
speed: ['kilometers_per_hour', 'miles_per_hour', 'meters_per_second'],
|
|
331
|
+
time: ['seconds', 'minutes', 'hours', 'days']
|
|
332
|
+
};
|
|
333
|
+
const unitsToShow = commonUnits[category] || [];
|
|
334
|
+
for (const unit of unitsToShow) {
|
|
335
|
+
if (unit !== excludeUnit && this.conversions[category][unit]) {
|
|
336
|
+
const value = baseValue * this.conversions[category][unit];
|
|
337
|
+
alternatives.push({
|
|
338
|
+
unit: unit.replace(/_/g, ' '),
|
|
339
|
+
value: parseFloat(value.toFixed(4))
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return alternatives;
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
exports.UnitConverterAction = UnitConverterAction;
|
|
347
|
+
exports.UnitConverterAction = UnitConverterAction = __decorate([
|
|
348
|
+
(0, global_1.RegisterClass)(actions_1.BaseAction, "__UnitConverter")
|
|
349
|
+
], UnitConverterAction);
|
|
350
|
+
/**
|
|
351
|
+
* Loader function to ensure the UnitConverterAction class is included in the bundle.
|
|
352
|
+
*/
|
|
353
|
+
function LoadUnitConverterAction() {
|
|
354
|
+
// this function is a stub that is used to force the bundler to include the above class in the final bundle and not tree shake them out
|
|
355
|
+
}
|
|
356
|
+
exports.LoadUnitConverterAction = LoadUnitConverterAction;
|
|
357
|
+
//# sourceMappingURL=unit-converter.action.js.map
|