ut2 1.3.1 → 1.4.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.
Files changed (64) hide show
  1. package/README.md +2 -4
  2. package/dist/ut2.js +124 -69
  3. package/dist/ut2.js.map +1 -1
  4. package/dist/ut2.min.js +1 -1
  5. package/dist/ut2.min.js.map +1 -1
  6. package/es/index.js +1 -0
  7. package/es/internals/checkType.js +11 -0
  8. package/es/internals/compare.js +8 -26
  9. package/es/internals/helpers.js +5 -6
  10. package/es/internals/native.js +10 -1
  11. package/es/isArguments.js +4 -4
  12. package/es/isArrayBuffer.js +3 -3
  13. package/es/isBlob.js +3 -3
  14. package/es/isBoolean.js +3 -2
  15. package/es/isDataView.js +3 -2
  16. package/es/isDate.js +3 -3
  17. package/es/isEqual.js +12 -10
  18. package/es/isError.js +3 -5
  19. package/es/isFunction.js +3 -2
  20. package/es/isMap.js +3 -3
  21. package/es/isMatch.js +71 -0
  22. package/es/isNumber.js +3 -3
  23. package/es/isPlainObject.js +3 -3
  24. package/es/isRegExp.js +3 -3
  25. package/es/isSet.js +3 -3
  26. package/es/isString.js +3 -3
  27. package/es/isSymbol.js +3 -3
  28. package/es/isTypedArray.js +3 -2
  29. package/es/isWeakMap.js +3 -3
  30. package/es/isWeakSet.js +3 -3
  31. package/lib/index.js +2 -0
  32. package/lib/internals/checkType.js +14 -0
  33. package/lib/internals/compare.js +8 -26
  34. package/lib/internals/helpers.js +3 -4
  35. package/lib/internals/native.js +9 -0
  36. package/lib/isArguments.js +2 -2
  37. package/lib/isArrayBuffer.js +3 -3
  38. package/lib/isBlob.js +2 -2
  39. package/lib/isBoolean.js +3 -2
  40. package/lib/isDataView.js +3 -2
  41. package/lib/isDate.js +3 -3
  42. package/lib/isEqual.js +12 -10
  43. package/lib/isError.js +3 -5
  44. package/lib/isFunction.js +3 -2
  45. package/lib/isMap.js +3 -3
  46. package/lib/isMatch.js +73 -0
  47. package/lib/isNumber.js +3 -3
  48. package/lib/isPlainObject.js +2 -2
  49. package/lib/isRegExp.js +3 -3
  50. package/lib/isSet.js +3 -3
  51. package/lib/isString.js +3 -3
  52. package/lib/isSymbol.js +3 -3
  53. package/lib/isTypedArray.js +3 -2
  54. package/lib/isWeakMap.js +3 -3
  55. package/lib/isWeakSet.js +3 -3
  56. package/package.json +1 -1
  57. package/types/index.d.ts +1 -0
  58. package/types/internals/checkType.d.ts +2 -0
  59. package/types/internals/native.d.ts +6 -0
  60. package/types/isBuffer.d.ts +1 -1
  61. package/types/isMatch.d.ts +44 -0
  62. package/es/internals/isType.js +0 -11
  63. package/lib/internals/isType.js +0 -13
  64. package/types/internals/isType.d.ts +0 -11
package/README.md CHANGED
@@ -1,10 +1,8 @@
1
1
  # ut2
2
2
 
3
- [![npm][npm]][npm-url] [![Build and Deploy Docs](https://github.com/caijf/ut2/actions/workflows/ci.yml/badge.svg)](https://github.com/caijf/ut2/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/caijf/ut2/branch/main/graph/badge.svg?token=XKTS0H7085)](https://codecov.io/gh/caijf/ut2) ![npm](https://img.shields.io/npm/dt/ut2) ![GitHub](https://img.shields.io/github/license/caijf/ut2.svg)
4
-
5
- 一个现代 JavaScript 实用工具库。
3
+ 一个现代 JavaScript 实用工具库。[点击查看在线文档]
6
4
 
7
- [点击查看在线文档]
5
+ [![npm][npm]][npm-url] [![Build and Deploy Docs](https://github.com/caijf/ut2/actions/workflows/ci.yml/badge.svg)](https://github.com/caijf/ut2/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/caijf/ut2/branch/main/graph/badge.svg?token=XKTS0H7085)](https://codecov.io/gh/caijf/ut2) ![npm](https://img.shields.io/npm/dt/ut2) ![GitHub](https://img.shields.io/github/license/caijf/ut2.svg)
8
6
 
9
7
  ## 比较
10
8
 
package/dist/ut2.js CHANGED
@@ -43,6 +43,9 @@
43
43
  }
44
44
  return '';
45
45
  }
46
+ function wrapTags(tags) {
47
+ return tags.map(function (item) { return '[object ' + item + ']'; });
48
+ }
46
49
  var numberTag = '[object Number]';
47
50
  var booleanTag = '[object Boolean]';
48
51
  var stringTag = '[object String]';
@@ -53,6 +56,12 @@
53
56
  var arrayBufferTag = '[object ArrayBuffer]';
54
57
  var argumentsTag = '[object Arguments]';
55
58
  var arrayTag = '[object Array]';
59
+ var typedArrayTags = wrapTags(['Float32Array', 'Float64Array', 'Int8Array', 'Int16Array', 'Int32Array', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'BigInt64Array', 'BigUint64Array']);
60
+ var functionTags = wrapTags(['Function', 'AsyncFunction', 'GeneratorFunction', 'Proxy']);
61
+ var weakSetTag = '[object WeakSet]';
62
+ var blobTag = '[object Blob]';
63
+ var fileTag = '[object Blob]';
64
+ var domExceptionTag = '[object DOMException]';
56
65
  var objectTag = '[object Object]';
57
66
  var dataViewTag = '[object DataView]';
58
67
  var mapTag = '[object Map]';
@@ -129,20 +138,16 @@
129
138
  }
130
139
  var getTag$1 = getTag;
131
140
 
132
- function isType(value, type) {
133
- var nativeTypeString = getTag$1(value);
134
- if (typeof type === 'string') {
135
- return nativeTypeString === '[object ' + type + ']';
136
- }
137
- return type.some(function (item) { return nativeTypeString === '[object ' + item + ']'; });
141
+ function checkType(value, tag) {
142
+ return getTag$1(value) === tag;
138
143
  }
139
-
140
- function isObjectLike(value) {
141
- return value != null && typeof value === 'object';
144
+ function checkTypes(value, tags) {
145
+ var tag = getTag$1(value);
146
+ return tags.some(function (item) { return tag === item; });
142
147
  }
143
148
 
144
149
  function isSymbol(value) {
145
- return typeof value === 'symbol' || (isObjectLike(value) && isType(value, 'Symbol'));
150
+ return typeof value === 'symbol' || checkType(value, symbolTag);
146
151
  }
147
152
 
148
153
  var reIsBinary = /^0b[01]+$/i;
@@ -273,21 +278,20 @@
273
278
  });
274
279
  }
275
280
 
276
- var argType = 'Arguments';
277
- var supportedArgumentsType = isType((function () { return arguments; })(), argType);
281
+ var supportedArgumentsType = checkType((function () { return arguments; })(), argumentsTag);
278
282
  var FUNC_ERROR_TEXT = 'Expected a function';
279
283
  var numberIsFinite = Number.isFinite;
280
284
  var numberIsInteger = Number.isInteger;
281
285
  var numberIsSafeInteger = Number.isSafeInteger;
282
286
  var objectGetOwnPropertySymbols = Object.getOwnPropertySymbols;
283
287
  var arrayAt = arrayProto.at;
284
- var VERSION = "1.3.1";
288
+ var VERSION = "1.4.0";
285
289
 
286
290
  function isFunction(value) {
287
291
  if (!isObject(value)) {
288
292
  return false;
289
293
  }
290
- return isType(value, ['Function', 'AsyncFunction', 'GeneratorFunction', 'Proxy']);
294
+ return checkTypes(value, functionTags);
291
295
  }
292
296
 
293
297
  function isLength(value) {
@@ -375,6 +379,10 @@
375
379
  return uniq(array.concat(other), iteratee, strickCheck);
376
380
  }
377
381
 
382
+ function isObjectLike(value) {
383
+ return value != null && typeof value === 'object';
384
+ }
385
+
378
386
  function isArrayLikeObject(value) {
379
387
  return isObjectLike(value) && isArrayLike(value);
380
388
  }
@@ -494,21 +502,12 @@
494
502
  function compareAsc(value, other) {
495
503
  var valueIsSymbol = isSymbol(value);
496
504
  var otherIsSymbol = isSymbol(other);
497
- if (valueIsSymbol && otherIsSymbol) {
498
- return 0;
499
- }
500
- if (valueIsSymbol) {
501
- return 1;
502
- }
503
- if (otherIsSymbol) {
504
- return -1;
505
- }
506
- var valStr = toString(value);
507
- var othStr = toString(other);
508
- if (valStr > othStr) {
505
+ var valueStr = toString(value);
506
+ var otherStr = toString(other);
507
+ if (!otherIsSymbol && (valueIsSymbol || valueStr > otherStr)) {
509
508
  return 1;
510
509
  }
511
- if (valStr < othStr) {
510
+ if (!valueIsSymbol && (otherIsSymbol || valueStr < otherStr)) {
512
511
  return -1;
513
512
  }
514
513
  return 0;
@@ -516,21 +515,12 @@
516
515
  function compareDesc(value, other) {
517
516
  var valueIsSymbol = isSymbol(value);
518
517
  var otherIsSymbol = isSymbol(other);
519
- if (valueIsSymbol && otherIsSymbol) {
520
- return 0;
521
- }
522
- if (valueIsSymbol) {
518
+ var valueStr = toString(value);
519
+ var otherStr = toString(other);
520
+ if (!otherIsSymbol && (valueIsSymbol || valueStr > otherStr)) {
523
521
  return -1;
524
522
  }
525
- if (otherIsSymbol) {
526
- return 1;
527
- }
528
- var valStr = toString(value);
529
- var othStr = toString(other);
530
- if (valStr > othStr) {
531
- return -1;
532
- }
533
- if (valStr < othStr) {
523
+ if (!valueIsSymbol && (otherIsSymbol || valueStr < otherStr)) {
534
524
  return 1;
535
525
  }
536
526
  return 0;
@@ -754,7 +744,7 @@
754
744
 
755
745
  function isArguments(value) {
756
746
  if (supportedArgumentsType) {
757
- return isObjectLike(value) && isType(value, argType);
747
+ return checkType(value, argumentsTag);
758
748
  }
759
749
  return isObjectLike(value) && hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee');
760
750
  }
@@ -782,15 +772,15 @@
782
772
  var nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray;
783
773
 
784
774
  function isArrayBuffer(value) {
785
- return nodeIsArrayBuffer ? nodeIsArrayBuffer(value) : isObjectLike(value) && isType(value, 'ArrayBuffer');
775
+ return nodeIsArrayBuffer ? nodeIsArrayBuffer(value) : checkType(value, arrayBufferTag);
786
776
  }
787
777
 
788
778
  function isBlob(value) {
789
- return (blobExisted && value instanceof Blob) || isType(value, ['Blob', 'File']);
779
+ return (blobExisted && value instanceof Blob) || checkTypes(value, [blobTag, fileTag]);
790
780
  }
791
781
 
792
782
  function isBoolean(value) {
793
- return value === true || value === false || isType(value, 'Boolean');
783
+ return value === true || value === false || checkType(value, booleanTag);
794
784
  }
795
785
 
796
786
  function isBuffer(value) {
@@ -801,15 +791,15 @@
801
791
  }
802
792
 
803
793
  function isDataView(value) {
804
- return isType(value, 'DataView');
794
+ return checkType(value, dataViewTag);
805
795
  }
806
796
 
807
797
  function isDate(value) {
808
- return nodeIsDate ? nodeIsDate(value) : isObjectLike(value) && isType(value, 'Date');
798
+ return nodeIsDate ? nodeIsDate(value) : checkType(value, dateTag);
809
799
  }
810
800
 
811
801
  function isPlainObject(value) {
812
- if (!isObjectLike(value) || !isType(value, 'Object')) {
802
+ if (!isObjectLike(value) || !checkType(value, objectTag)) {
813
803
  return false;
814
804
  }
815
805
  var proto = Object.getPrototypeOf(Object(value));
@@ -847,11 +837,11 @@
847
837
  }
848
838
 
849
839
  function isMap(value) {
850
- return nodeIsMap ? nodeIsMap(value) : isObjectLike(value) && isType(value, 'Map');
840
+ return nodeIsMap ? nodeIsMap(value) : checkType(value, mapTag);
851
841
  }
852
842
 
853
843
  function isSet(value) {
854
- return nodeIsSet ? nodeIsSet(value) : isObjectLike(value) && isType(value, 'Set');
844
+ return nodeIsSet ? nodeIsSet(value) : checkType(value, setTag);
855
845
  }
856
846
 
857
847
  function isEmpty(value) {
@@ -894,7 +884,7 @@
894
884
  if (nodeIsTypedArray) {
895
885
  return nodeIsTypedArray(value);
896
886
  }
897
- return isObjectLike(value) && isLength(value.length) && isType(value, ['Float32Array', 'Float64Array', 'Int8Array', 'Int16Array', 'Int32Array', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'BigInt64Array', 'BigUint64Array']);
887
+ return isObjectLike(value) && isLength(value.length) && checkTypes(value, typedArrayTags);
898
888
  }
899
889
 
900
890
  function mapToArray(map) {
@@ -1010,13 +1000,14 @@
1010
1000
  if (hasCustomizer) {
1011
1001
  var compared = customizer(value[length], other[length], length, value, other, valueStack, otherStack);
1012
1002
  if (compared !== undefined) {
1013
- result = !!compared;
1014
- break;
1003
+ if (!compared) {
1004
+ return false;
1005
+ }
1006
+ continue;
1015
1007
  }
1016
1008
  }
1017
1009
  if (!isEqualDeep(value[length], other[length], customizer, strictCheck, valueStack, otherStack)) {
1018
- result = false;
1019
- break;
1010
+ return false;
1020
1011
  }
1021
1012
  }
1022
1013
  }
@@ -1032,23 +1023,24 @@
1032
1023
  if (hasCustomizer) {
1033
1024
  var compared = customizer(value[key], other[key], key, value, other, valueStack, otherStack);
1034
1025
  if (compared !== undefined) {
1035
- result = !!compared;
1036
- break;
1026
+ if (!compared) {
1027
+ return false;
1028
+ }
1029
+ continue;
1037
1030
  }
1038
1031
  }
1039
1032
  if (!(hasOwnProperty.call(other, key) && isEqualDeep(value[key], other[key], customizer, strictCheck, valueStack, otherStack))) {
1040
- result = false;
1041
- break;
1033
+ return false;
1042
1034
  }
1043
1035
  if (!skipCtor && key === 'constructor') {
1044
1036
  skipCtor = true;
1045
1037
  }
1046
1038
  }
1047
- if (result && !skipCtor) {
1039
+ if (!skipCtor) {
1048
1040
  var valCtor = value.constructor;
1049
1041
  var othCtor = other.constructor;
1050
1042
  if (valCtor !== othCtor && !(isFunction(valCtor) && valCtor instanceof valCtor && isFunction(othCtor) && othCtor instanceof othCtor) && 'constructor' in value && 'constructor' in other) {
1051
- result = false;
1043
+ return false;
1052
1044
  }
1053
1045
  }
1054
1046
  }
@@ -1071,10 +1063,7 @@
1071
1063
  }
1072
1064
 
1073
1065
  function isError(value) {
1074
- if (!isObjectLike(value)) {
1075
- return false;
1076
- }
1077
- return value instanceof Error || isType(value, ['Error', 'DOMException']);
1066
+ return isObjectLike(value) && (value instanceof Error || checkTypes(value, [errorTag, domExceptionTag]));
1078
1067
  }
1079
1068
 
1080
1069
  function isFinite(value) {
@@ -1085,8 +1074,73 @@
1085
1074
  return numberIsInteger ? numberIsInteger(value) : isFinite(value) && Math.floor(value) === value;
1086
1075
  }
1087
1076
 
1077
+ function isDeepComparable(object, source) {
1078
+ return checkType(object, objectTag) && checkType(source, objectTag);
1079
+ }
1080
+ function baseIsMatch(object, source, customizer, strictCheck, objStack, srcStack) {
1081
+ var hasCustomizer = typeof customizer === 'function';
1082
+ if (isDeepComparable(object, source)) {
1083
+ objStack = objStack || [];
1084
+ srcStack = srcStack || [];
1085
+ var stackLen = objStack.length;
1086
+ while (stackLen--) {
1087
+ if (objStack[stackLen] === object && srcStack[stackLen] === source) {
1088
+ return true;
1089
+ }
1090
+ }
1091
+ objStack.push(object);
1092
+ srcStack.push(source);
1093
+ var keys = allKeys(source);
1094
+ var length_1 = keys.length;
1095
+ while (length_1--) {
1096
+ var key = keys[length_1];
1097
+ if (!(key in object)) {
1098
+ return false;
1099
+ }
1100
+ var compared = void 0;
1101
+ if (hasCustomizer) {
1102
+ compared = customizer(object[key], source[key], key, object, source, objStack, srcStack);
1103
+ }
1104
+ if (compared !== undefined) {
1105
+ if (!compared) {
1106
+ return false;
1107
+ }
1108
+ continue;
1109
+ }
1110
+ if (!baseIsMatch(object[key], source[key], customizer, strictCheck, objStack, srcStack)) {
1111
+ return false;
1112
+ }
1113
+ }
1114
+ objStack.pop();
1115
+ srcStack.pop();
1116
+ return true;
1117
+ }
1118
+ var result = isEqual(object, source, function (objValue, srcValue, k, obj, src) {
1119
+ if (hasCustomizer) {
1120
+ var compared = customizer(objValue, srcValue, k, obj, src, objStack, srcStack);
1121
+ if (compared !== undefined) {
1122
+ return compared;
1123
+ }
1124
+ }
1125
+ if (isDeepComparable(objValue, srcValue)) {
1126
+ return baseIsMatch(objValue, srcValue, customizer, strictCheck, objStack, srcStack);
1127
+ }
1128
+ }, strictCheck);
1129
+ return result;
1130
+ }
1131
+ function isMatch(object, source, customizer, strictCheck) {
1132
+ if (strictCheck === void 0) { strictCheck = false; }
1133
+ if (typeof customizer === 'function' && isDeepComparable(object, source)) {
1134
+ var compared = customizer(object, source);
1135
+ if (compared !== undefined) {
1136
+ return !!compared;
1137
+ }
1138
+ }
1139
+ return baseIsMatch(object, source, customizer, strictCheck);
1140
+ }
1141
+
1088
1142
  function isNumber(value) {
1089
- return typeof value === 'number' || (isObjectLike(value) && isType(value, 'Number'));
1143
+ return typeof value === 'number' || checkType(value, numberTag);
1090
1144
  }
1091
1145
 
1092
1146
  function isNaN(value) {
@@ -1102,7 +1156,7 @@
1102
1156
  }
1103
1157
 
1104
1158
  function isRegExp(value) {
1105
- return nodeIsRegExp ? nodeIsRegExp(value) : isObjectLike(value) && isType(value, 'RegExp');
1159
+ return nodeIsRegExp ? nodeIsRegExp(value) : checkType(value, regExpTag);
1106
1160
  }
1107
1161
 
1108
1162
  function isSafeInteger(value) {
@@ -1110,7 +1164,7 @@
1110
1164
  }
1111
1165
 
1112
1166
  function isString(value) {
1113
- return typeof value === 'string' || (isObjectLike(value) && isType(value, 'String'));
1167
+ return typeof value === 'string' || checkType(value, stringTag);
1114
1168
  }
1115
1169
 
1116
1170
  function isUndefined(value) {
@@ -1118,11 +1172,11 @@
1118
1172
  }
1119
1173
 
1120
1174
  function isWeakMap(value) {
1121
- return isObjectLike(value) && isType(value, 'WeakMap');
1175
+ return checkType(value, weakMapTag);
1122
1176
  }
1123
1177
 
1124
1178
  function isWeakSet(value) {
1125
- return isObjectLike(value) && isType(value, 'WeakSet');
1179
+ return checkType(value, weakSetTag);
1126
1180
  }
1127
1181
 
1128
1182
  function decimalAdjust(type, value, precision) {
@@ -1625,6 +1679,7 @@
1625
1679
  exports.isInteger = isInteger;
1626
1680
  exports.isLength = isLength;
1627
1681
  exports.isMap = isMap;
1682
+ exports.isMatch = isMatch;
1628
1683
  exports.isNaN = isNaN;
1629
1684
  exports.isNil = isNil;
1630
1685
  exports.isNull = isNull;