@indra.ai/deva 1.2.38 → 1.3.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/index.js +59 -300
- package/lib/index.js +262 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Copyright (c)2025 Quinn Michaels; All Rights Reserved; Legal Signature Required.
|
|
2
|
-
// Distributed under the
|
|
2
|
+
// Distributed under the Restricted software license, see the accompanying file LICENSE.md
|
|
3
3
|
import {EventEmitter} from 'node:events';
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
|
|
4
|
+
import {randomUUID} from 'crypto';
|
|
5
|
+
|
|
6
|
+
import lib from './lib/index.js';
|
|
7
|
+
|
|
8
8
|
|
|
9
9
|
import Config from './config.json' with {type:'json'};
|
|
10
10
|
const config = Config.DATA;
|
|
@@ -21,11 +21,8 @@ class Deva {
|
|
|
21
21
|
this._support = false; // inherited Support features.
|
|
22
22
|
this._services = false; // inherited Service features.
|
|
23
23
|
this._systems = false; // inherited Service features.
|
|
24
|
-
this.os = os; // It is used to provide basic operating system related utility functions.
|
|
25
|
-
this.fs = fs; // this is so file system functions are in the core.
|
|
26
|
-
this.path = path; // this is so we can get path in the system.
|
|
27
24
|
this.events = opts.events || new EventEmitter({}); // Event Bus
|
|
28
|
-
this.
|
|
25
|
+
this.lib = new lib({}); // used for loading library functions
|
|
29
26
|
this.utils = opts.utils || {}; // parse function
|
|
30
27
|
this.devas = opts.devas || {}; // Devas which are loaded
|
|
31
28
|
this.vars = opts.vars || {}; // Variables object
|
|
@@ -168,7 +165,7 @@ class Deva {
|
|
|
168
165
|
***************/
|
|
169
166
|
_methodNotFound(packet) {
|
|
170
167
|
packet.a = {
|
|
171
|
-
id: this.uid(),
|
|
168
|
+
id: this.lib.uid(),
|
|
172
169
|
agent: this.agent() || false,
|
|
173
170
|
client: this.client() || false,
|
|
174
171
|
text: `${this._messages.method_not_found}`,
|
|
@@ -203,7 +200,7 @@ class Deva {
|
|
|
203
200
|
}
|
|
204
201
|
}
|
|
205
202
|
}
|
|
206
|
-
const _client = this.copy(client); // copy the client parameter
|
|
203
|
+
const _client = this.lib.copy(client); // copy the client parameter
|
|
207
204
|
this._client = _client; // set local _client to this scope
|
|
208
205
|
return Promise.resolve();
|
|
209
206
|
}
|
|
@@ -226,7 +223,7 @@ class Deva {
|
|
|
226
223
|
const {id, profile, features} = _cl; // make a copy the clinet data.
|
|
227
224
|
const {security} = features; // make a copy the clinet data.
|
|
228
225
|
this._security = { // set this_security with data
|
|
229
|
-
id: this.uid(true), // uuid of the security feature
|
|
226
|
+
id: this.lib.uid(true), // uuid of the security feature
|
|
230
227
|
client_id: id, // client id for reference
|
|
231
228
|
client_name: profile.name, // client name for personalization
|
|
232
229
|
hash: security.hash, // client preferred hash algorithm
|
|
@@ -262,7 +259,7 @@ class Deva {
|
|
|
262
259
|
const {id, features, profile} = _cl; // set the local consts from client copy
|
|
263
260
|
const {support} = features; // set support from features const
|
|
264
261
|
this._support = { // set this_support with data
|
|
265
|
-
id: this.uid(true), // uuid of the support feature
|
|
262
|
+
id: this.lib.uid(true), // uuid of the support feature
|
|
266
263
|
client_id: id, // client id for reference
|
|
267
264
|
client_name: profile.name, // client name for personalization
|
|
268
265
|
concerns: support.concerns, // any concerns for client
|
|
@@ -296,7 +293,7 @@ class Deva {
|
|
|
296
293
|
const {id, features, profile} = _cl; // set the local consts from client copy
|
|
297
294
|
const {services} = features; // set services from features const
|
|
298
295
|
this._services = { // set this_services with data
|
|
299
|
-
id: this.uid(true), // uuid of the services feature
|
|
296
|
+
id: this.lib.uid(true), // uuid of the services feature
|
|
300
297
|
client_id: id, // client id for reference
|
|
301
298
|
client_name: profile.name, // client name for personalization
|
|
302
299
|
concerns: services.concerns, // any concerns for client
|
|
@@ -330,7 +327,7 @@ class Deva {
|
|
|
330
327
|
const {id, features, profile} = _cl; // set the local consts from client copy
|
|
331
328
|
const {services} = features; // set services from features const
|
|
332
329
|
this._services = { // set this_services with data
|
|
333
|
-
id: this.uid(true), // uuid of the services feature
|
|
330
|
+
id: this.lib.uid(true), // uuid of the services feature
|
|
334
331
|
client_id: id, // client id for reference
|
|
335
332
|
client_name: profile.name, // client name for personalization
|
|
336
333
|
concerns: services.concerns, // any concerns for client
|
|
@@ -429,7 +426,7 @@ class Deva {
|
|
|
429
426
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
430
427
|
this.zone('question');
|
|
431
428
|
|
|
432
|
-
const id = this.uid(); // generate a unique id for transport.
|
|
429
|
+
const id = this.lib.uid(); // generate a unique id for transport.
|
|
433
430
|
const t_split = TEXT.split(' '); // split the text on spaces to get words.
|
|
434
431
|
const data = DATA; // set the DATA to data
|
|
435
432
|
|
|
@@ -475,7 +472,7 @@ class Deva {
|
|
|
475
472
|
|
|
476
473
|
this.state('set', `question:${method}`)
|
|
477
474
|
packet.q = { // build packet.q container
|
|
478
|
-
id: this.uid(), // set the transport id for the question.
|
|
475
|
+
id: this.lib.uid(), // set the transport id for the question.
|
|
479
476
|
agent: this.agent(), // set the agent
|
|
480
477
|
client: this.client(), // set the client
|
|
481
478
|
meta: { // build the meta container
|
|
@@ -489,10 +486,10 @@ class Deva {
|
|
|
489
486
|
}
|
|
490
487
|
|
|
491
488
|
// hash the question
|
|
492
|
-
packet.q.meta.hash = this.hash(packet.q);
|
|
489
|
+
packet.q.meta.hash = this.lib.hash(packet.q);
|
|
493
490
|
|
|
494
491
|
this.action('talk', config.events.question);
|
|
495
|
-
this.talk(config.events.question, this.copy(packet)); // global question event make sure to copy data.
|
|
492
|
+
this.talk(config.events.question, this.lib.copy(packet)); // global question event make sure to copy data.
|
|
496
493
|
|
|
497
494
|
if (isAsk) { // isAsk check if the question isAsk and talk
|
|
498
495
|
// if: isAsk wait for the once event which is key'd to the packet ID for specified responses
|
|
@@ -500,7 +497,7 @@ class Deva {
|
|
|
500
497
|
this.talk(`${key}:ask`, packet);
|
|
501
498
|
this.once(`${key}:ask:${packet.id}`, answer => {
|
|
502
499
|
this.action('talk', config.events.ask);
|
|
503
|
-
this.talk(config.events.ask, this.copy(answer));
|
|
500
|
+
this.talk(config.events.ask, this.lib.copy(answer));
|
|
504
501
|
return this.finish(answer, resolve); // if:isAsk resolve the answer from the call
|
|
505
502
|
});
|
|
506
503
|
}
|
|
@@ -547,7 +544,7 @@ class Deva {
|
|
|
547
544
|
|
|
548
545
|
this.state('set', 'answer')
|
|
549
546
|
const packet_answer = { // setup the packet.a container
|
|
550
|
-
id: this.uid(),
|
|
547
|
+
id: this.lib.uid(),
|
|
551
548
|
agent, // set the agent who answered the question
|
|
552
549
|
client, // set the client asking the question
|
|
553
550
|
meta: { // setup the answer meta container
|
|
@@ -562,10 +559,10 @@ class Deva {
|
|
|
562
559
|
};
|
|
563
560
|
|
|
564
561
|
// create a hash for the answer and insert into answer meta.
|
|
565
|
-
packet_answer.meta.hash = this.hash(packet_answer);
|
|
562
|
+
packet_answer.meta.hash = this.lib.hash(packet_answer);
|
|
566
563
|
packet.a = packet_answer; // set the packet.a to the packet_answer
|
|
567
564
|
this.action('talk', config.events.answer)
|
|
568
|
-
this.talk(config.events.answer, this.copy(packet)); // global talk event
|
|
565
|
+
this.talk(config.events.answer, this.lib.copy(packet)); // global talk event
|
|
569
566
|
this.state('resovle', 'answer');
|
|
570
567
|
return this.finish(packet, resolve); // resolve the packet to the caller.
|
|
571
568
|
}).catch(err => { // catch any errors in the method
|
|
@@ -598,7 +595,7 @@ class Deva {
|
|
|
598
595
|
const client = this.client();
|
|
599
596
|
// build the answer packet from this model
|
|
600
597
|
const packet_answer = {
|
|
601
|
-
id: this.uid(),
|
|
598
|
+
id: this.lib.uid(),
|
|
602
599
|
agent,
|
|
603
600
|
client,
|
|
604
601
|
meta: {
|
|
@@ -633,9 +630,9 @@ class Deva {
|
|
|
633
630
|
}
|
|
634
631
|
|
|
635
632
|
this.state('set', `ask:${method}`);
|
|
636
|
-
packet_answer.meta.hash = this.hash(packet_answer);
|
|
633
|
+
packet_answer.meta.hash = this.lib.hash(packet_answer);
|
|
637
634
|
packet.a = packet_answer;
|
|
638
|
-
this.talk(config.events.answer, this.copy(packet)); // global talk event
|
|
635
|
+
this.talk(config.events.answer, this.lib.copy(packet)); // global talk event
|
|
639
636
|
this.talk(`${agent.key}:ask:${packet.id}`, packet);
|
|
640
637
|
}).catch(err => {
|
|
641
638
|
this.talk(`${agent.key}:ask:${packet.id}`, {error:err});
|
|
@@ -671,7 +668,7 @@ class Deva {
|
|
|
671
668
|
const agent = this.agent();
|
|
672
669
|
|
|
673
670
|
const _data = {
|
|
674
|
-
id: this.uid(true),
|
|
671
|
+
id: this.lib.uid(true),
|
|
675
672
|
key: 'init',
|
|
676
673
|
value: agent.key,
|
|
677
674
|
agent,
|
|
@@ -679,7 +676,7 @@ class Deva {
|
|
|
679
676
|
text: this._messages.init,
|
|
680
677
|
created: Date.now(),
|
|
681
678
|
}
|
|
682
|
-
_data.hash = this.hash(_data);
|
|
679
|
+
_data.hash = this.lib.hash(_data);
|
|
683
680
|
return new Promise((resolve, reject) => {
|
|
684
681
|
this.events.setMaxListeners(this.maxListeners);
|
|
685
682
|
this._assignInherit().then(() => {
|
|
@@ -724,7 +721,7 @@ class Deva {
|
|
|
724
721
|
this.action('start');
|
|
725
722
|
data.value = 'start';
|
|
726
723
|
delete data.hash;
|
|
727
|
-
data.hash = this.hash(data);
|
|
724
|
+
data.hash = this.lib.hash(data);
|
|
728
725
|
const hasOnStart = this.onStart && typeof this.onStart === 'function' ? true : false;
|
|
729
726
|
this.state('start');
|
|
730
727
|
return hasOnStart ? this.onStart(data, resolve) : this.enter(data, resolve)
|
|
@@ -747,7 +744,7 @@ class Deva {
|
|
|
747
744
|
this.action('enter');
|
|
748
745
|
data.value = 'enter';
|
|
749
746
|
delete data.hash;
|
|
750
|
-
data.hash = this.hash(data);
|
|
747
|
+
data.hash = this.lib.hash(data);
|
|
751
748
|
this.state('enter');
|
|
752
749
|
const hasOnEnter = this.onEnter && typeof this.onEnter === 'function' ? true : false;
|
|
753
750
|
return hasOnEnter ? this.onEnter(data, resolve) : this.done(data, resolve)
|
|
@@ -769,7 +766,7 @@ class Deva {
|
|
|
769
766
|
this.action('done');
|
|
770
767
|
data.value = 'done';
|
|
771
768
|
delete data.hash;
|
|
772
|
-
data.hash = this.hash(data);
|
|
769
|
+
data.hash = this.lib.hash(data);
|
|
773
770
|
const hasOnDone = this.onDone && typeof this.onDone === 'function' ? true : false;
|
|
774
771
|
this.state('done');
|
|
775
772
|
return hasOnDone ? this.onDone(data, resolve) : this.ready(data, resolve);
|
|
@@ -788,7 +785,7 @@ class Deva {
|
|
|
788
785
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
789
786
|
this.action('ready'); // set the complete action
|
|
790
787
|
|
|
791
|
-
packet.hash = this.hash(packet);// hash the entire packet before completeing.
|
|
788
|
+
packet.hash = this.lib.hash(packet);// hash the entire packet before completeing.
|
|
792
789
|
// check for agent on complete function in agent
|
|
793
790
|
const hasOnReady = this.onReady && typeof this.onReady === 'function';
|
|
794
791
|
|
|
@@ -813,7 +810,7 @@ class Deva {
|
|
|
813
810
|
finish(packet, resolve) {
|
|
814
811
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
815
812
|
this.action('finish'); // set the finish action
|
|
816
|
-
packet.hash = this.hash(packet);// hash the entire packet before finishing.
|
|
813
|
+
packet.hash = this.lib.hash(packet);// hash the entire packet before finishing.
|
|
817
814
|
// check for agent on finish function in agent
|
|
818
815
|
const hasOnFinish = this.onFinish && typeof this.onFinish === 'function';
|
|
819
816
|
|
|
@@ -837,7 +834,7 @@ class Deva {
|
|
|
837
834
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
838
835
|
this.action('complete'); // set the complete action
|
|
839
836
|
|
|
840
|
-
packet.hash = this.hash(packet);// hash the entire packet before completeing.
|
|
837
|
+
packet.hash = this.lib.hash(packet);// hash the entire packet before completeing.
|
|
841
838
|
// check for agent on complete function in agent
|
|
842
839
|
const hasOnComplete = this.onComplete && typeof this.onComplete === 'function';
|
|
843
840
|
|
|
@@ -865,7 +862,7 @@ class Deva {
|
|
|
865
862
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
866
863
|
this.state('stop'); // set the state to stop
|
|
867
864
|
const data = { // build the stop data
|
|
868
|
-
id: this.uid(), // set the id
|
|
865
|
+
id: this.lib.uid(), // set the id
|
|
869
866
|
agent: this.agent(), // set the agent
|
|
870
867
|
client: this.client(), // set the client
|
|
871
868
|
key: 'stop', // set the key
|
|
@@ -902,14 +899,14 @@ class Deva {
|
|
|
902
899
|
|
|
903
900
|
this.action('exit');
|
|
904
901
|
const data = {
|
|
905
|
-
id: this.uid(true),
|
|
902
|
+
id: this.lib.uid(true),
|
|
906
903
|
key: 'exit',
|
|
907
904
|
value: this._messages.exit,
|
|
908
905
|
agent,
|
|
909
906
|
client,
|
|
910
907
|
created: Date.now(),
|
|
911
908
|
}
|
|
912
|
-
data.hash = this.hash(data);
|
|
909
|
+
data.hash = this.lib.hash(data);
|
|
913
910
|
|
|
914
911
|
// clear memory
|
|
915
912
|
this._active = false;
|
|
@@ -939,7 +936,7 @@ class Deva {
|
|
|
939
936
|
const lookup = this._states[value]; // set the local states lookup
|
|
940
937
|
const text = extra ? `${lookup} ${extra}` : lookup; // set text from lookup with extra
|
|
941
938
|
const data = { // build the data object
|
|
942
|
-
id: this.uid(), // set the data id
|
|
939
|
+
id: this.lib.uid(), // set the data id
|
|
943
940
|
agent: this.agent(), // set the agent
|
|
944
941
|
client: this.client(), // set the client
|
|
945
942
|
key: 'state', // set the key to state
|
|
@@ -947,7 +944,7 @@ class Deva {
|
|
|
947
944
|
text, // set the text value of the data
|
|
948
945
|
created: Date.now(), // set the data created date.
|
|
949
946
|
};
|
|
950
|
-
data.hash = this.hash(data); // hash the data
|
|
947
|
+
data.hash = this.lib.hash(data); // hash the data
|
|
951
948
|
this.talk(config.events.state, data); // broadcasat the state event
|
|
952
949
|
return data;
|
|
953
950
|
} catch (e) { // catch any errors
|
|
@@ -964,7 +961,7 @@ class Deva {
|
|
|
964
961
|
this.action('func', 'states');
|
|
965
962
|
this.state('return', 'states');
|
|
966
963
|
return {
|
|
967
|
-
id: this.uid(true),
|
|
964
|
+
id: this.lib.uid(true),
|
|
968
965
|
key: 'states',
|
|
969
966
|
value: this._states,
|
|
970
967
|
created: Date.now(),
|
|
@@ -986,7 +983,7 @@ class Deva {
|
|
|
986
983
|
const text = extra ? `${lookup} ${extra}` : lookup; // set the text value
|
|
987
984
|
|
|
988
985
|
const data = { // build the zone data
|
|
989
|
-
id: this.uid(), // set the packetid
|
|
986
|
+
id: this.lib.uid(), // set the packetid
|
|
990
987
|
agent: this.agent(),
|
|
991
988
|
client: this.client(),
|
|
992
989
|
key: 'zone',
|
|
@@ -994,7 +991,7 @@ class Deva {
|
|
|
994
991
|
text,
|
|
995
992
|
created: Date.now(),
|
|
996
993
|
};
|
|
997
|
-
data.hash = this.hash(data);
|
|
994
|
+
data.hash = this.lib.hash(data);
|
|
998
995
|
this.talk(config.events.zone, data);
|
|
999
996
|
} catch (e) {
|
|
1000
997
|
return this.error(e);
|
|
@@ -1010,7 +1007,7 @@ class Deva {
|
|
|
1010
1007
|
this.action('func', 'zones');
|
|
1011
1008
|
this.state('return', 'zones');
|
|
1012
1009
|
return {
|
|
1013
|
-
id: this.uid(true), // set the uuid of the data
|
|
1010
|
+
id: this.lib.uid(true), // set the uuid of the data
|
|
1014
1011
|
agent: this.agent(), // set the agent value
|
|
1015
1012
|
cleint: this.client(), // set the client value
|
|
1016
1013
|
key: 'zones', // set the key return value
|
|
@@ -1037,7 +1034,7 @@ class Deva {
|
|
|
1037
1034
|
const msg = msg_action || action; // set the correct message
|
|
1038
1035
|
const text = extra ? `${msg} ${extra}` : msg; // set the text of the action
|
|
1039
1036
|
const data = { // build the data object for the action.
|
|
1040
|
-
id: this.uid(true), // generate a guid for the action transmitssion.
|
|
1037
|
+
id: this.lib.uid(true), // generate a guid for the action transmitssion.
|
|
1041
1038
|
agent: this.agent(), // the agent data to send with the action
|
|
1042
1039
|
client: this.client(), // the client data to send with the action
|
|
1043
1040
|
key: 'action', // the key for event to transmit action type
|
|
@@ -1045,7 +1042,7 @@ class Deva {
|
|
|
1045
1042
|
text, // text of the action to send
|
|
1046
1043
|
created: Date.now(), // action time stamp
|
|
1047
1044
|
};
|
|
1048
|
-
data.hash = this.hash(data); // generate a hash of the action packet.
|
|
1045
|
+
data.hash = this.lib.hash(data); // generate a hash of the action packet.
|
|
1049
1046
|
this.talk(config.events.action, data); // talk the core action event
|
|
1050
1047
|
return data;
|
|
1051
1048
|
} catch (e) { // catch any errors that occur
|
|
@@ -1062,7 +1059,7 @@ class Deva {
|
|
|
1062
1059
|
this.action('func', 'actions');
|
|
1063
1060
|
this.state('return', 'actions');
|
|
1064
1061
|
return {
|
|
1065
|
-
id: this.uid(true), // set the id with a uuid
|
|
1062
|
+
id: this.lib.uid(true), // set the id with a uuid
|
|
1066
1063
|
agent: this.agent(), // set the agent value
|
|
1067
1064
|
client: this.client(), // set the client value
|
|
1068
1065
|
key: 'actions', // set the data key
|
|
@@ -1087,14 +1084,14 @@ class Deva {
|
|
|
1087
1084
|
const text = extra ? `${lookup} ${extra}` : lookup; // set the text value
|
|
1088
1085
|
|
|
1089
1086
|
const data = { // build data object
|
|
1090
|
-
id: this.uid(true), // set the id
|
|
1087
|
+
id: this.lib.uid(true), // set the id
|
|
1091
1088
|
agent: this.agent(), // set the agent transporting the packet.
|
|
1092
1089
|
key: 'feature', // set the key for transport
|
|
1093
1090
|
value, // set the value of the key
|
|
1094
1091
|
text, // set the text value
|
|
1095
1092
|
created: Date.now(), // set the creation date
|
|
1096
1093
|
};
|
|
1097
|
-
data.hash = this.hash(data); // generate the hash value of the data packet
|
|
1094
|
+
data.hash = this.lib.hash(data); // generate the hash value of the data packet
|
|
1098
1095
|
this.talk(config.events.feature, data); // talk the feature event with data
|
|
1099
1096
|
return data;
|
|
1100
1097
|
} catch (e) { // catch any errors
|
|
@@ -1110,7 +1107,7 @@ class Deva {
|
|
|
1110
1107
|
features() {
|
|
1111
1108
|
this.state('return', 'features');
|
|
1112
1109
|
return { // return the data object
|
|
1113
|
-
id: this.uid(true), // set the object id
|
|
1110
|
+
id: this.lib.uid(true), // set the object id
|
|
1114
1111
|
agent: this.agent(), // set the agent value.
|
|
1115
1112
|
client: this.client(), // set the client value.
|
|
1116
1113
|
key: 'features', // set the key
|
|
@@ -1134,7 +1131,7 @@ class Deva {
|
|
|
1134
1131
|
const text = extra ? `${lookup} ${extra}` : lookup;
|
|
1135
1132
|
|
|
1136
1133
|
const data = {
|
|
1137
|
-
id: this.uid(true),
|
|
1134
|
+
id: this.lib.uid(true),
|
|
1138
1135
|
agent: this.agent(),
|
|
1139
1136
|
client: this.client(),
|
|
1140
1137
|
key: 'context',
|
|
@@ -1143,7 +1140,7 @@ class Deva {
|
|
|
1143
1140
|
created: Date.now(),
|
|
1144
1141
|
};
|
|
1145
1142
|
|
|
1146
|
-
data.hash = this.hash(data);
|
|
1143
|
+
data.hash = this.lib.hash(data);
|
|
1147
1144
|
this.talk(config.events.context, data);
|
|
1148
1145
|
return data;
|
|
1149
1146
|
} catch (e) {
|
|
@@ -1155,7 +1152,7 @@ class Deva {
|
|
|
1155
1152
|
if (!this._active) return this._messages.offline; // check the active status
|
|
1156
1153
|
this.state('return', 'contexts');
|
|
1157
1154
|
return {
|
|
1158
|
-
id: this.uid(true),
|
|
1155
|
+
id: this.lib.uid(true),
|
|
1159
1156
|
agent: this.agent(),
|
|
1160
1157
|
client: this.client(),
|
|
1161
1158
|
key: 'contexts',
|
|
@@ -1172,7 +1169,7 @@ class Deva {
|
|
|
1172
1169
|
***************/
|
|
1173
1170
|
client() {
|
|
1174
1171
|
if (!this._active) return this._messages.offline; // check the active status
|
|
1175
|
-
const client_copy = this.copy(this._client); // create a copy of the client data
|
|
1172
|
+
const client_copy = this.lib.copy(this._client); // create a copy of the client data
|
|
1176
1173
|
return client_copy; // return the copy of the client data.
|
|
1177
1174
|
}
|
|
1178
1175
|
|
|
@@ -1184,7 +1181,7 @@ class Deva {
|
|
|
1184
1181
|
***************/
|
|
1185
1182
|
agent() {
|
|
1186
1183
|
if (!this._active) return this._messages.offline; // check the active status
|
|
1187
|
-
const agent_copy = this.copy(this._agent); // create a copy of the agent data.
|
|
1184
|
+
const agent_copy = this.lib.copy(this._agent); // create a copy of the agent data.
|
|
1188
1185
|
return agent_copy; // return the copy of the agent data.
|
|
1189
1186
|
}
|
|
1190
1187
|
|
|
@@ -1201,7 +1198,7 @@ class Deva {
|
|
|
1201
1198
|
this.feature('security'); // set the security state
|
|
1202
1199
|
try {
|
|
1203
1200
|
this.state('return', 'security'); // set the security state
|
|
1204
|
-
return this.copy(this._security); // return the security feature
|
|
1201
|
+
return this.lib.copy(this._security); // return the security feature
|
|
1205
1202
|
} catch (e) {return this.error(e);}
|
|
1206
1203
|
}
|
|
1207
1204
|
|
|
@@ -1217,7 +1214,7 @@ class Deva {
|
|
|
1217
1214
|
this.feature('support'); // set the support state
|
|
1218
1215
|
try {
|
|
1219
1216
|
this.state('return', 'support'); // set the action to support.
|
|
1220
|
-
return this.copy(this._support); // return the support feature
|
|
1217
|
+
return this.lib.copy(this._support); // return the support feature
|
|
1221
1218
|
} catch (e) {
|
|
1222
1219
|
return this.error(e); // return this.error when error catch
|
|
1223
1220
|
}
|
|
@@ -1235,7 +1232,7 @@ class Deva {
|
|
|
1235
1232
|
this.feature('services'); // set the support state
|
|
1236
1233
|
try {
|
|
1237
1234
|
this.state('return', 'services'); // set the services state
|
|
1238
|
-
return this.copy(this._services); // return the services feature
|
|
1235
|
+
return this.lib.copy(this._services); // return the services feature
|
|
1239
1236
|
} catch (e) {
|
|
1240
1237
|
return this.error(e); // return this.error when error catch
|
|
1241
1238
|
}
|
|
@@ -1253,7 +1250,7 @@ class Deva {
|
|
|
1253
1250
|
this.feature('systems'); // set the support state
|
|
1254
1251
|
try {
|
|
1255
1252
|
this.state('return', 'systems'); // set the systems state
|
|
1256
|
-
return this.copy(this._systems); // return the systems feature
|
|
1253
|
+
return this.lib.copy(this._systems); // return the systems feature
|
|
1257
1254
|
} catch (e) {
|
|
1258
1255
|
return this.error(e); // return this.error when error catch
|
|
1259
1256
|
}
|
|
@@ -1296,92 +1293,6 @@ class Deva {
|
|
|
1296
1293
|
}
|
|
1297
1294
|
|
|
1298
1295
|
|
|
1299
|
-
// UTILITY FUNCTIONS
|
|
1300
|
-
/**************
|
|
1301
|
-
func: uid
|
|
1302
|
-
params:
|
|
1303
|
-
- guid: This is a true false flag for generating a guid.
|
|
1304
|
-
describe:
|
|
1305
|
-
The uid function can create two types of id for you.
|
|
1306
|
-
1. random GUID - this is good for when you need a uinique record id returned
|
|
1307
|
-
2. transport id - The transport id is a number generated to provide a
|
|
1308
|
-
numerical number used for transporting records to places
|
|
1309
|
-
like social networks, email, other networks where informaton
|
|
1310
|
-
is shared.
|
|
1311
|
-
***************/
|
|
1312
|
-
uid(guid=false) {
|
|
1313
|
-
let id;
|
|
1314
|
-
if (guid) {
|
|
1315
|
-
id = randomUUID()
|
|
1316
|
-
}
|
|
1317
|
-
else {
|
|
1318
|
-
const min = Math.floor(Date.now() - (Date.now() / Math.PI));
|
|
1319
|
-
const max = Math.floor(Date.now() + (Date.now() * Math.PI));
|
|
1320
|
-
id = Math.floor(Math.random() * (max - min)) + min;
|
|
1321
|
-
}
|
|
1322
|
-
return id;
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
|
-
/**************
|
|
1326
|
-
func: hash
|
|
1327
|
-
params:
|
|
1328
|
-
- texts: The text string to create a hash value for.
|
|
1329
|
-
- algo: The hashing algorithm to use for hashing. md5, sha256, or sha512
|
|
1330
|
-
|
|
1331
|
-
describe:
|
|
1332
|
-
The hash algorithm will take a string of text and produce a hash.
|
|
1333
|
-
***************/
|
|
1334
|
-
hash(str, algo=false) {
|
|
1335
|
-
// this.action('hash');
|
|
1336
|
-
algo = algo || this._security.hash || 'md5';
|
|
1337
|
-
const the_hash = createHash(algo);
|
|
1338
|
-
the_hash.update(JSON.stringify(str));
|
|
1339
|
-
const _digest = the_hash.digest('base64');
|
|
1340
|
-
return `${algo}-${_digest}`;
|
|
1341
|
-
}
|
|
1342
|
-
|
|
1343
|
-
/**************
|
|
1344
|
-
func: cipher
|
|
1345
|
-
params: str - string to encrypt
|
|
1346
|
-
describe:
|
|
1347
|
-
The encrypt function allows for the internal encryption of data based on the
|
|
1348
|
-
defined client security settings.
|
|
1349
|
-
***************/
|
|
1350
|
-
cipher(str) {
|
|
1351
|
-
this.feature('cipher');
|
|
1352
|
-
// this.action('cipher');
|
|
1353
|
-
const security = this._security;
|
|
1354
|
-
const {password, algorithm} = security.cipher;
|
|
1355
|
-
const key = createHash('sha256').update(String(password)).digest('base64');
|
|
1356
|
-
const key_in_bytes = Buffer.from(key, 'base64')
|
|
1357
|
-
const iv = randomBytes(16);
|
|
1358
|
-
// create a new cipher
|
|
1359
|
-
const _cipher = createCipheriv(algorithm, key_in_bytes, iv);
|
|
1360
|
-
const encrypted = _cipher.update(String(str), 'utf8', 'hex') + _cipher.final('hex');
|
|
1361
|
-
|
|
1362
|
-
this.state('return', 'cipher');
|
|
1363
|
-
return {
|
|
1364
|
-
iv: iv.toString('base64'),
|
|
1365
|
-
key,
|
|
1366
|
-
encrypted,
|
|
1367
|
-
}
|
|
1368
|
-
}
|
|
1369
|
-
|
|
1370
|
-
decipher(opt) {
|
|
1371
|
-
this.feature('decipher');
|
|
1372
|
-
// this.action('decipher');
|
|
1373
|
-
const iv = Buffer.from(opt.iv, 'base64');
|
|
1374
|
-
const encrypted = Buffer.from(opt.encrypted, 'hex');
|
|
1375
|
-
const key_in_bytes = Buffer.from(opt.key, 'base64')
|
|
1376
|
-
const security = this._security;
|
|
1377
|
-
const {algorithm} = security.cipher;
|
|
1378
|
-
const decipher = createDecipheriv( algorithm, key_in_bytes, iv);
|
|
1379
|
-
const decrypted = decipher.update(encrypted);
|
|
1380
|
-
const final = Buffer.concat([decrypted, decipher.final()]);
|
|
1381
|
-
return final.toString();
|
|
1382
|
-
this.state('return', 'decipher');
|
|
1383
|
-
}
|
|
1384
|
-
|
|
1385
1296
|
/**************
|
|
1386
1297
|
func: prompt
|
|
1387
1298
|
params:
|
|
@@ -1397,7 +1308,7 @@ class Deva {
|
|
|
1397
1308
|
const agent = this.agent();
|
|
1398
1309
|
const client = this.client();
|
|
1399
1310
|
const _data = {
|
|
1400
|
-
id: this.uid(true),
|
|
1311
|
+
id: this.lib.uid(true),
|
|
1401
1312
|
key: 'prompt',
|
|
1402
1313
|
value: agent.key,
|
|
1403
1314
|
agent,
|
|
@@ -1409,161 +1320,9 @@ class Deva {
|
|
|
1409
1320
|
}
|
|
1410
1321
|
|
|
1411
1322
|
|
|
1412
|
-
/**************
|
|
1413
|
-
func: copy
|
|
1414
|
-
params: obj
|
|
1415
|
-
describe:
|
|
1416
|
-
a simple copy object to create a memory clean copy of data to
|
|
1417
|
-
prevent collisions when needed. Handles clean text, array, object copy.
|
|
1418
|
-
it makes the assumption tha the user is submitting either an array or object
|
|
1419
|
-
for copying.
|
|
1420
|
-
***************/
|
|
1421
|
-
copy(obj) {
|
|
1422
|
-
return JSON.parse(JSON.stringify(obj));
|
|
1423
|
-
}
|
|
1424
|
-
|
|
1425
|
-
/**************
|
|
1426
|
-
func: getToday
|
|
1427
|
-
params:
|
|
1428
|
-
- d: The date string to get the day of..
|
|
1429
|
-
describe:
|
|
1430
|
-
a date can be passed in or generated to produce a date string for the day
|
|
1431
|
-
where time is 0. This feature is useful for logging for getting a date
|
|
1432
|
-
with no time value for the current day.
|
|
1433
|
-
***************/
|
|
1434
|
-
getToday(d) {
|
|
1435
|
-
this.feature('getToday');
|
|
1436
|
-
d = d ? d : Date.now();
|
|
1437
|
-
const today = new Date(d);
|
|
1438
|
-
today.setHours(0);
|
|
1439
|
-
today.setMinutes(0);
|
|
1440
|
-
today.setSeconds(0);
|
|
1441
|
-
today.setMilliseconds(0);
|
|
1442
|
-
this.state('return', 'getToday');
|
|
1443
|
-
return today.getTime();
|
|
1444
|
-
}
|
|
1445
|
-
|
|
1446
|
-
/**************
|
|
1447
|
-
func: formatDate
|
|
1448
|
-
params:
|
|
1449
|
-
- d: The date string to format.
|
|
1450
|
-
- format: the various formats that can be selected.
|
|
1451
|
-
- time: boolean flag to include the time stampt iwth the date.
|
|
1452
|
-
- locale: The locale formatting of the date to return.
|
|
1453
|
-
describe:
|
|
1454
|
-
formats: long, long_month, short, short_month, year, month, day
|
|
1455
|
-
FDate format ensures that consistent date formatting is used within the
|
|
1456
|
-
system based on the language and locale in the client profile.
|
|
1457
|
-
***************/
|
|
1458
|
-
formatDate(d, format='milli', time=false) {
|
|
1459
|
-
this.feature('formatDate');
|
|
1460
|
-
if (!d) d = Date.now();
|
|
1461
|
-
d = new Date(d);
|
|
1462
|
-
|
|
1463
|
-
if (format === 'milli') return d.getTime();
|
|
1464
|
-
// pre-set date formats for returning user dates.
|
|
1465
|
-
const formats = {
|
|
1466
|
-
long: { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' },
|
|
1467
|
-
long_month: { year: 'numeric', month: 'long', day: 'numeric'},
|
|
1468
|
-
short: { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' },
|
|
1469
|
-
short_month: { year: 'numeric', month: 'short', day: 'numeric' },
|
|
1470
|
-
numeric: { year: 'numeric', month: 'numeric', day: 'numeric' },
|
|
1471
|
-
year: { year: 'numeric' },
|
|
1472
|
-
month: { month: 'long' },
|
|
1473
|
-
day: { day: 'long' },
|
|
1474
|
-
log: { year: 'numeric', month: 'short', day: 'numeric' },
|
|
1475
|
-
};
|
|
1476
|
-
const theDate = d.toLocaleDateString(this._client.profile.locale, formats[format]);
|
|
1477
|
-
const theTime = time ? this.formatTime(d) : false;
|
|
1478
|
-
this.state('return', 'formatDate');
|
|
1479
|
-
return !theTime ? theDate : `${theDate} - ${theTime}`;
|
|
1480
|
-
}
|
|
1481
|
-
|
|
1482
|
-
/**************
|
|
1483
|
-
func: formatTime
|
|
1484
|
-
params:
|
|
1485
|
-
- t: the time to format
|
|
1486
|
-
describe:
|
|
1487
|
-
The formatTime fucntion will return a consistent local time for the t
|
|
1488
|
-
parameter based on the locale setting in the client profile..
|
|
1489
|
-
***************/
|
|
1490
|
-
formatTime(t) {
|
|
1491
|
-
this.feature('formatTime');
|
|
1492
|
-
return t.toLocaleTimeString(this._client.profile.locale); // return the formatted time string
|
|
1493
|
-
}
|
|
1494
|
-
|
|
1495
|
-
/**************
|
|
1496
|
-
func: formatCurrency
|
|
1497
|
-
params:
|
|
1498
|
-
- n: is the number that you want to return the currency of.
|
|
1499
|
-
describe:
|
|
1500
|
-
The formatCurrency function will format a currency value based on the setting
|
|
1501
|
-
in the client profile.
|
|
1502
|
-
***************/
|
|
1503
|
-
formatCurrency(n, cur=false) {
|
|
1504
|
-
this.feature('formatCurrency');
|
|
1505
|
-
const currency = cur || this._client.profile.currency;
|
|
1506
|
-
return new Intl.NumberFormat(this._client.profile.locale, { style: 'currency', currency: currency }).format(n);
|
|
1507
|
-
}
|
|
1508
|
-
|
|
1509
|
-
/**************
|
|
1510
|
-
func: formatCurrency
|
|
1511
|
-
params:
|
|
1512
|
-
- n: is the number that you want to return the currency of.
|
|
1513
|
-
describe:
|
|
1514
|
-
The formatCurrency function will format a currency value based on the setting
|
|
1515
|
-
in the client profile.
|
|
1516
|
-
***************/
|
|
1517
|
-
formatNumber(n) {
|
|
1518
|
-
this.feature('formatNumber');
|
|
1519
|
-
return new Intl.NumberFormat(this._client.profile.locale).format(n);
|
|
1520
|
-
}
|
|
1521
1323
|
|
|
1522
|
-
/**************
|
|
1523
|
-
func: formatPercent
|
|
1524
|
-
params:
|
|
1525
|
-
- n: is the number that you want to format as a percent.
|
|
1526
|
-
- dec: is the number of decimal places to apply to the number.
|
|
1527
|
-
describe:
|
|
1528
|
-
***************/
|
|
1529
|
-
formatPercent(n, dec=2) {
|
|
1530
|
-
this.feature('formatPercent');
|
|
1531
|
-
return parseFloat(n).toFixed(dec) + '%';
|
|
1532
|
-
}
|
|
1533
1324
|
|
|
1534
|
-
/**************
|
|
1535
|
-
func: trimWords
|
|
1536
|
-
params:
|
|
1537
|
-
- text: The text to trim.
|
|
1538
|
-
- maxwords: The number of words to max.
|
|
1539
|
-
describe:
|
|
1540
|
-
A utility function to trimText intput to a specific word count.
|
|
1541
|
-
***************/
|
|
1542
|
-
trimWords(text, maxwords) {
|
|
1543
|
-
this.feature('trimWords');
|
|
1544
|
-
const splitter = text.split(' ');
|
|
1545
|
-
if (splitter < maxwords) return text;
|
|
1546
|
-
this.state('return', 'trimmed words');
|
|
1547
|
-
return splitter.slice(0, maxwords).join(' ');
|
|
1548
|
-
}
|
|
1549
1325
|
|
|
1550
|
-
/**************
|
|
1551
|
-
func: dupes
|
|
1552
|
-
params: dupers
|
|
1553
|
-
describe: remove duplicees from an array.
|
|
1554
|
-
***************/
|
|
1555
|
-
dupes(dupers) {
|
|
1556
|
-
this.feature('dupes');
|
|
1557
|
-
if (!Array.isArray(dupers)) return dupers;
|
|
1558
|
-
const check = [];
|
|
1559
|
-
this.state('return', 'duplicates removed');
|
|
1560
|
-
return dupers.filter(dupe => {
|
|
1561
|
-
if (!check.includes(dupe)) {
|
|
1562
|
-
check.push(dupe);
|
|
1563
|
-
return dupe;
|
|
1564
|
-
}
|
|
1565
|
-
});
|
|
1566
|
-
}
|
|
1567
1326
|
|
|
1568
1327
|
/**************
|
|
1569
1328
|
func: info
|
|
@@ -1594,7 +1353,7 @@ class Deva {
|
|
|
1594
1353
|
this.feature('status');
|
|
1595
1354
|
|
|
1596
1355
|
// format the date since active for output.
|
|
1597
|
-
const dateFormat = this.formatDate(this._active, 'long', true);
|
|
1356
|
+
const dateFormat = this.lib.formatDate(this._active, 'long', true);
|
|
1598
1357
|
// create the text msg string
|
|
1599
1358
|
return `${this._agent.profile.name} active since ${dateFormat}`; // return final text string
|
|
1600
1359
|
}
|
|
@@ -1648,7 +1407,7 @@ class Deva {
|
|
|
1648
1407
|
const agent = this.agent();
|
|
1649
1408
|
const client = this.client();
|
|
1650
1409
|
const _data = {
|
|
1651
|
-
id: this.uid(true),
|
|
1410
|
+
id: this.lib.uid(true),
|
|
1652
1411
|
key: 'error',
|
|
1653
1412
|
value: agent.key,
|
|
1654
1413
|
agent,
|
|
@@ -1657,7 +1416,7 @@ class Deva {
|
|
|
1657
1416
|
data,
|
|
1658
1417
|
created: Date.now(),
|
|
1659
1418
|
}
|
|
1660
|
-
this.talk(config.events.error, this.copy(_data));
|
|
1419
|
+
this.talk(config.events.error, this.lib.copy(_data));
|
|
1661
1420
|
const hasOnError = this.onError && typeof this.onError === 'function' ? true : false;
|
|
1662
1421
|
|
|
1663
1422
|
this.state('error');
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// Copyright (c)2025 Quinn Michaels
|
|
2
|
+
// Distributed under the MIT software license, see the accompanying
|
|
3
|
+
// file LICENSE.md or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import os from 'os';
|
|
7
|
+
|
|
8
|
+
import { createHash, randomUUID, createCipheriv, createDecipheriv, randomBytes } from 'crypto';
|
|
9
|
+
|
|
10
|
+
class Node {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
for (let opt in opts) this[opt] = opts[opt];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
class LIB {
|
|
16
|
+
constructor(opts) {
|
|
17
|
+
this.lang = opts.lang || 'en';
|
|
18
|
+
this.locale = opts.locale || 'en-US';
|
|
19
|
+
this.currency = opts.currency || 'USD';
|
|
20
|
+
this.os = os;
|
|
21
|
+
this.fs = fs;
|
|
22
|
+
this.path = path;
|
|
23
|
+
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
help(msg, help_dir) {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const params = msg.split(' ');
|
|
29
|
+
let helpFile = 'main';
|
|
30
|
+
if (params[0]) helpFile = params[0];
|
|
31
|
+
if (params[1]) helpFile = `${params[0]}_${params[1]}`;
|
|
32
|
+
helpFile = path.join(help_dir, 'help', `${helpFile}.feecting`);
|
|
33
|
+
try {
|
|
34
|
+
return resolve(fs.readFileSync(helpFile, 'utf8'))
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return reject(e)
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
/**************
|
|
43
|
+
func: uid
|
|
44
|
+
params:
|
|
45
|
+
- guid: This is a true false flag for generating a guid.
|
|
46
|
+
describe:
|
|
47
|
+
The uid function can create two types of id for you.
|
|
48
|
+
1. random GUID - this is good for when you need a uinique record id returned
|
|
49
|
+
2. transport id - The transport id is a number generated to provide a
|
|
50
|
+
numerical number used for transporting records to places
|
|
51
|
+
like social networks, email, other networks where informaton
|
|
52
|
+
is shared.
|
|
53
|
+
***************/
|
|
54
|
+
uid(guid=false) {
|
|
55
|
+
let id;
|
|
56
|
+
if (guid) {
|
|
57
|
+
id = randomUUID()
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const min = Math.floor(Date.now() - (Date.now() / Math.PI));
|
|
61
|
+
const max = Math.floor(Date.now() + (Date.now() * Math.PI));
|
|
62
|
+
id = Math.floor(Math.random() * (max - min)) + min;
|
|
63
|
+
}
|
|
64
|
+
return id;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**************
|
|
68
|
+
func: hash
|
|
69
|
+
params:
|
|
70
|
+
- texts: The text string to create a hash value for.
|
|
71
|
+
- algo: The hashing algorithm to use for hashing. md5, sha256, or sha512
|
|
72
|
+
|
|
73
|
+
describe:
|
|
74
|
+
The hash algorithm will take a string of text and produce a hash.
|
|
75
|
+
***************/
|
|
76
|
+
hash(str, algo=false) {
|
|
77
|
+
algo = algo || 'md5';
|
|
78
|
+
const the_hash = createHash(algo);
|
|
79
|
+
the_hash.update(JSON.stringify(str));
|
|
80
|
+
const _digest = the_hash.digest('base64');
|
|
81
|
+
return `${algo}-${_digest}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**************
|
|
85
|
+
func: cipher
|
|
86
|
+
params: str - string to encrypt
|
|
87
|
+
describe:
|
|
88
|
+
The encrypt function allows for the internal encryption of data based on the
|
|
89
|
+
defined client security settings.
|
|
90
|
+
***************/
|
|
91
|
+
cipher(str, opts) {
|
|
92
|
+
const {password, algorithm} = opts;
|
|
93
|
+
const key = createHash('sha256').update(String(password)).digest('base64');
|
|
94
|
+
const key_in_bytes = Buffer.from(key, 'base64')
|
|
95
|
+
const iv = randomBytes(16);
|
|
96
|
+
// create a new cipher
|
|
97
|
+
const _cipher = createCipheriv(algorithm, key_in_bytes, iv);
|
|
98
|
+
const encrypted = _cipher.update(String(str), 'utf8', 'hex') + _cipher.final('hex');
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
iv: iv.toString('base64'),
|
|
102
|
+
key,
|
|
103
|
+
encrypted,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
decipher(opt) {
|
|
108
|
+
const iv = Buffer.from(opt.iv, 'base64');
|
|
109
|
+
const encrypted = Buffer.from(opt.encrypted, 'hex');
|
|
110
|
+
const key_in_bytes = Buffer.from(opt.key, 'base64')
|
|
111
|
+
const decipher = createDecipheriv( 'aes-256-cbc', key_in_bytes, iv);
|
|
112
|
+
const decrypted = decipher.update(encrypted);
|
|
113
|
+
const final = Buffer.concat([decrypted, decipher.final()]);
|
|
114
|
+
return final.toString();
|
|
115
|
+
this.state('return', 'decipher');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**************
|
|
119
|
+
func: copy
|
|
120
|
+
params: obj
|
|
121
|
+
describe:
|
|
122
|
+
a simple copy object to create a memory clean copy of data to
|
|
123
|
+
prevent collisions when needed. Handles clean text, array, object copy.
|
|
124
|
+
it makes the assumption tha the user is submitting either an array or object
|
|
125
|
+
for copying.
|
|
126
|
+
***************/
|
|
127
|
+
copy(obj) {
|
|
128
|
+
return JSON.parse(JSON.stringify(obj));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**************
|
|
132
|
+
func: getToday
|
|
133
|
+
params:
|
|
134
|
+
- d: The date string to get the day of..
|
|
135
|
+
describe:
|
|
136
|
+
a date can be passed in or generated to produce a date string for the day
|
|
137
|
+
where time is 0. This feature is useful for logging for getting a date
|
|
138
|
+
with no time value for the current day.
|
|
139
|
+
***************/
|
|
140
|
+
getToday(d) {
|
|
141
|
+
d = d ? d : Date.now();
|
|
142
|
+
const today = new Date(d);
|
|
143
|
+
today.setHours(0);
|
|
144
|
+
today.setMinutes(0);
|
|
145
|
+
today.setSeconds(0);
|
|
146
|
+
today.setMilliseconds(0);
|
|
147
|
+
return today.getTime();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**************
|
|
151
|
+
func: formatDate
|
|
152
|
+
params:
|
|
153
|
+
- d: The date string to format.
|
|
154
|
+
- format: the various formats that can be selected.
|
|
155
|
+
- time: boolean flag to include the time stampt iwth the date.
|
|
156
|
+
- locale: The locale formatting of the date to return.
|
|
157
|
+
describe:
|
|
158
|
+
formats: long, long_month, short, short_month, year, month, day
|
|
159
|
+
FDate format ensures that consistent date formatting is used within the
|
|
160
|
+
system based on the language and locale in the client profile.
|
|
161
|
+
***************/
|
|
162
|
+
formatDate(d, format='milli', time=false) {
|
|
163
|
+
if (!d) d = Date.now();
|
|
164
|
+
d = new Date(d);
|
|
165
|
+
|
|
166
|
+
if (format === 'milli') return d.getTime();
|
|
167
|
+
// pre-set date formats for returning user dates.
|
|
168
|
+
const formats = {
|
|
169
|
+
long: { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' },
|
|
170
|
+
long_month: { year: 'numeric', month: 'long', day: 'numeric'},
|
|
171
|
+
short: { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' },
|
|
172
|
+
short_month: { year: 'numeric', month: 'short', day: 'numeric' },
|
|
173
|
+
numeric: { year: 'numeric', month: 'numeric', day: 'numeric' },
|
|
174
|
+
year: { year: 'numeric' },
|
|
175
|
+
month: { month: 'long' },
|
|
176
|
+
day: { day: 'long' },
|
|
177
|
+
log: { year: 'numeric', month: 'short', day: 'numeric' },
|
|
178
|
+
};
|
|
179
|
+
const theDate = d.toLocaleDateString(this.locale, formats[format]);
|
|
180
|
+
const theTime = time ? this.formatTime(d) : false;
|
|
181
|
+
return !theTime ? theDate : `${theDate} - ${theTime}`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**************
|
|
185
|
+
func: formatTime
|
|
186
|
+
params:
|
|
187
|
+
- t: the time to format
|
|
188
|
+
describe:
|
|
189
|
+
The formatTime fucntion will return a consistent local time for the t
|
|
190
|
+
parameter based on the locale setting in the client profile..
|
|
191
|
+
***************/
|
|
192
|
+
formatTime(t) {
|
|
193
|
+
return t.toLocaleTimeString(this.locale); // return the formatted time string
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**************
|
|
197
|
+
func: formatCurrency
|
|
198
|
+
params:
|
|
199
|
+
- n: is the number that you want to return the currency of.
|
|
200
|
+
describe:
|
|
201
|
+
The formatCurrency function will format a currency value based on the setting
|
|
202
|
+
in the client profile.
|
|
203
|
+
***************/
|
|
204
|
+
formatCurrency(n, cur=false) {
|
|
205
|
+
const currency = cur || this.currency;
|
|
206
|
+
return new Intl.NumberFormat(this.locale, { style: 'currency', currency: currency }).format(n);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**************
|
|
210
|
+
func: formatCurrency
|
|
211
|
+
params:
|
|
212
|
+
- n: is the number that you want to return the currency of.
|
|
213
|
+
describe:
|
|
214
|
+
The formatCurrency function will format a currency value based on the setting
|
|
215
|
+
in the client profile.
|
|
216
|
+
***************/
|
|
217
|
+
formatNumber(n) {
|
|
218
|
+
return new Intl.NumberFormat(this.locale).format(n);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**************
|
|
222
|
+
func: formatPercent
|
|
223
|
+
params:
|
|
224
|
+
- n: is the number that you want to format as a percent.
|
|
225
|
+
- dec: is the number of decimal places to apply to the number.
|
|
226
|
+
describe:
|
|
227
|
+
***************/
|
|
228
|
+
formatPercent(n, dec=2) {
|
|
229
|
+
return parseFloat(n).toFixed(dec) + '%';
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**************
|
|
233
|
+
func: trimWords
|
|
234
|
+
params:
|
|
235
|
+
- text: The text to trim.
|
|
236
|
+
- maxwords: The number of words to max.
|
|
237
|
+
describe:
|
|
238
|
+
A utility function to trimText intput to a specific word count.
|
|
239
|
+
***************/
|
|
240
|
+
trimWords(text, maxwords) {
|
|
241
|
+
const splitter = text.split(' ');
|
|
242
|
+
if (splitter < maxwords) return text;
|
|
243
|
+
return splitter.slice(0, maxwords).join(' ');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**************
|
|
247
|
+
func: dupes
|
|
248
|
+
params: dupers
|
|
249
|
+
describe: remove duplicees from an array.
|
|
250
|
+
***************/
|
|
251
|
+
dupes(dupers) {
|
|
252
|
+
if (!Array.isArray(dupers)) return dupers;
|
|
253
|
+
const check = [];
|
|
254
|
+
return dupers.filter(dupe => {
|
|
255
|
+
if (!check.includes(dupe)) {
|
|
256
|
+
check.push(dupe);
|
|
257
|
+
return dupe;
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
export default LIB
|