best-globals 1.0.4 → 1.1.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/README.md CHANGED
@@ -64,6 +64,12 @@ console.log(coalesce(undefined,coalesce.throwErrorIfUndefined('name'))); // = th
64
64
 
65
65
  Returns a new object like originalConfig with the changes reflected
66
66
 
67
+ Changes can be:
68
+ * any value,
69
+ * an object to apply the recursive changes (if the original is also an object)
70
+ * a call to `changes.trueByObject(changes)` that means that the change only applies to non falsy values
71
+
72
+
67
73
 
68
74
  ```js
69
75
  var changing = require('best-globals').changing;
package/best-globals.js CHANGED
@@ -117,8 +117,15 @@ function deepCopy(object){
117
117
 
118
118
  bestGlobals.deepCopy = deepCopy;
119
119
 
120
- bestGlobals.changing = function changing(original, changes){
121
- var opts = bestGlobals.changing.retreiveOptions(arguments);
120
+ function ChangingWithSpecial(change){
121
+ this.change = change;
122
+ }
123
+
124
+ function changing(original, changes){
125
+ var opts = changing.retreiveOptions(arguments);
126
+ if (changes instanceof ChangingWithSpecial) {
127
+ return changes.change(original);
128
+ }
122
129
  if(original===null ||
123
130
  !bestGlobals.isPlainObject(original) &&
124
131
  !(original instanceof Error) &&
@@ -144,7 +151,7 @@ bestGlobals.changing = function changing(original, changes){
144
151
  }else if('deletingValue' in opts && changes[name]===opts.deletingValue){
145
152
  // empty
146
153
  }else{
147
- result[name] = changing(original[name], changes[name], bestGlobals.changing.options(opts), true);
154
+ result[name] = changing(original[name], changes[name], changing.options(opts), true);
148
155
  }
149
156
  }
150
157
  for(name in changes){
@@ -158,6 +165,16 @@ bestGlobals.changing = function changing(original, changes){
158
165
  }
159
166
  };
160
167
 
168
+ changing.trueByObject = function trueByObject(object){
169
+ return new ChangingWithSpecial(function(original, opts){
170
+ if (original == true) return deepCopy(object);
171
+ if (original) return changing(original, object, changing.options(opts))
172
+ return original;
173
+ });
174
+ }
175
+
176
+ console.log('***********!',changing.trueByObject,changing)
177
+
161
178
  bestGlobals.dig = function dig(description, wanted){
162
179
  if(arguments.length==1){
163
180
  wanted=description;
@@ -253,7 +270,9 @@ bestGlobals.dig.array = function array(description, wanted){
253
270
  bestGlobals.spec = bestGlobals.dig;
254
271
 
255
272
 
256
- bestGlobals.createOptionsToFunction(bestGlobals.changing);
273
+ bestGlobals.createOptionsToFunction(changing);
274
+
275
+ bestGlobals.changing = changing;
257
276
 
258
277
  function npad(num, width) {
259
278
  var n=num+''; // to string
@@ -461,7 +480,7 @@ bestGlobals.Datetime.prototype.getTime = function getTime() {
461
480
  ).getTime();
462
481
  };
463
482
 
464
- bestGlobals.Datetime.prototype.toPlainString = function toPlainString(){
483
+ bestGlobals.Datetime.prototype.toPlainString = function toPlainString(preserveHm){
465
484
  var str=this.toYmdHmsMm();
466
485
  var prune = function(what){
467
486
  if(str.substr(str.length-what.length)==what){
@@ -471,7 +490,7 @@ bestGlobals.Datetime.prototype.toPlainString = function toPlainString(){
471
490
  return false;
472
491
  }
473
492
  /* eslint no-unused-expressions: 0 */
474
- prune('000') && prune('.000') && prune(':00') && prune(' 00:00');
493
+ prune('000') && prune('.000') && prune(':00') && (preserveHm || prune(' 00:00'));
475
494
  return str;
476
495
  }
477
496
  // bestGlobals.Datetime.prototype.toUTCString = function toUTCString(){ return this.iso; }
@@ -855,12 +874,26 @@ bestGlobals.registerJson4All = function registerJson4All(JSON4all){
855
874
  }else{
856
875
  return date.getTime();
857
876
  }
858
- }
877
+ },
878
+ serialize: function serialize(date){
879
+ return date.isRealDate ? date.toYmd() : date.toISOString();
880
+ },
881
+ deserialize: JSON4all.DateFunctions.deserialize,
882
+ valueLike:true
859
883
  });
860
884
  JSON4all.addType('date', {
861
885
  construct: function construct(value){
862
886
  return bestGlobals.date.iso(value);
863
887
  },
888
+ serialize: function serialize(o){
889
+ return o.toYmd();
890
+ },
891
+ deserialize: function deserialize(plainValue){
892
+ var ok = /^\d{4}-\d\d-\d\d$/.test(plainValue);
893
+ var value = ok && bestGlobals.date.iso(plainValue) || null;
894
+ return {ok, value};
895
+ },
896
+ valueLike:true
864
897
  });
865
898
  JSON4all.addType(bestGlobals.Datetime, {
866
899
  construct: function construct(value){
@@ -869,6 +902,15 @@ bestGlobals.registerJson4All = function registerJson4All(JSON4all){
869
902
  deconstruct: function construct(datetime){
870
903
  return datetime.toPlainString();
871
904
  },
905
+ serialize: function serialize(datetime){
906
+ return datetime.toPlainString(true);
907
+ },
908
+ deserialize: function deserialize(plainValue){
909
+ var ok = /^\d{4}-\d\d-\d\d \d\d:\d\d(:\d\d(.\d+)?)?$/.test(plainValue);
910
+ var value = ok && bestGlobals.datetime.iso(plainValue) || null;
911
+ return {ok, value};
912
+ },
913
+ valueLike:true
872
914
  });
873
915
  };
874
916
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "best-globals",
3
- "description": "common global function and constants - i.e. coalesce",
4
- "version": "1.0.4",
3
+ "description": "common global function and constants - i.e. changes",
4
+ "version": "1.1.1",
5
5
  "author": "Codenautas <codenautas@googlegroups.com>",
6
6
  "license": "MIT",
7
7
  "repository": "codenautas/best-globals",
@@ -22,8 +22,8 @@
22
22
  "expect.js": "~0.3.1",
23
23
  "express": "^4.18.2",
24
24
  "express-session": "^1.17.3",
25
- "karma": "^6.4.1",
26
- "karma-chrome-launcher": "^3.1.1",
25
+ "karma": "^6.4.2",
26
+ "karma-chrome-launcher": "^3.2.0",
27
27
  "karma-coverage": "^2.2.0",
28
28
  "karma-coverage-html-index-reporter": "^1.0.2",
29
29
  "karma-expect": "^1.1.3",
@@ -31,10 +31,10 @@
31
31
  "karma-mocha": "^2.0.1",
32
32
  "nyc": "~15.1.0",
33
33
  "mocha": "~10.2.0",
34
- "require-bro": "^0.3.0",
35
- "sinon": "~15.0.1",
34
+ "require-bro": "^0.3.1",
35
+ "sinon": "~15.1.0",
36
36
  "audit-copy": "~0.1.0",
37
- "discrepances": "~0.2.6"
37
+ "discrepances": "~0.2.8"
38
38
  },
39
39
  "main": "best-globals.js",
40
40
  "types": "best-globals.d.ts",