zero-query 0.9.1 → 0.9.5
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 +10 -7
- package/cli/commands/dev/devtools/js/elements.js +5 -0
- package/cli/scaffold/app/app.js +1 -1
- package/cli/scaffold/global.css +1 -2
- package/cli/scaffold/index.html +2 -1
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +363 -30
- package/dist/zquery.min.js +2 -2
- package/index.d.ts +46 -2
- package/index.js +32 -4
- package/package.json +1 -1
- package/src/component.js +32 -3
- package/src/core.js +3 -1
- package/src/expression.js +103 -16
- package/src/http.js +6 -2
- package/src/router.js +6 -1
- package/src/utils.js +191 -5
- package/tests/audit.test.js +4030 -0
- package/tests/core.test.js +41 -0
- package/tests/expression.test.js +10 -10
- package/tests/utils.test.js +543 -1
- package/types/utils.d.ts +103 -0
package/tests/core.test.js
CHANGED
|
@@ -546,6 +546,47 @@ describe('query quick refs', () => {
|
|
|
546
546
|
expect(el.id).toBe('created');
|
|
547
547
|
expect(el.textContent).toBe('text');
|
|
548
548
|
});
|
|
549
|
+
|
|
550
|
+
// qs / qsa — raw DOM shortcuts
|
|
551
|
+
it('$.qs() returns raw element by CSS selector', () => {
|
|
552
|
+
const el = query.qs('#main');
|
|
553
|
+
expect(el).toBe(document.getElementById('main'));
|
|
554
|
+
expect(el).toBeInstanceOf(HTMLElement);
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it('$.qs() returns null for non-matching selector', () => {
|
|
558
|
+
expect(query.qs('#nonexistent')).toBeNull();
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
it('$.qs() scopes to context element', () => {
|
|
562
|
+
const sidebar = document.getElementById('sidebar');
|
|
563
|
+
const el = query.qs('.nav-item', sidebar);
|
|
564
|
+
expect(el.textContent).toBe('Home');
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
it('$.qsa() returns array of raw elements', () => {
|
|
568
|
+
const els = query.qsa('.text');
|
|
569
|
+
expect(Array.isArray(els)).toBe(true);
|
|
570
|
+
expect(els.length).toBe(3);
|
|
571
|
+
expect(els[0]).toBeInstanceOf(HTMLElement);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it('$.qsa() returns empty array for non-matching selector', () => {
|
|
575
|
+
const els = query.qsa('.nonexistent');
|
|
576
|
+
expect(els).toEqual([]);
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
it('$.qsa() scopes to context element', () => {
|
|
580
|
+
const nav = document.getElementById('nav');
|
|
581
|
+
const els = query.qsa('.nav-item', nav);
|
|
582
|
+
expect(els.length).toBe(3);
|
|
583
|
+
expect(els[0].textContent).toBe('Home');
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
it('$.qsa() result supports Array methods', () => {
|
|
587
|
+
const names = query.qsa('.nav-item').map(el => el.textContent);
|
|
588
|
+
expect(names).toEqual(['Home', 'About', 'Contact']);
|
|
589
|
+
});
|
|
549
590
|
});
|
|
550
591
|
|
|
551
592
|
|
package/tests/expression.test.js
CHANGED
|
@@ -871,19 +871,19 @@ describe('safeEval — new keyword', () => {
|
|
|
871
871
|
expect(result).toBeInstanceOf(Array);
|
|
872
872
|
});
|
|
873
873
|
|
|
874
|
-
it('Map/Set/RegExp
|
|
875
|
-
// Map, Set, RegExp are
|
|
876
|
-
|
|
877
|
-
expect(eval_('new
|
|
878
|
-
expect(eval_('new
|
|
879
|
-
expect(eval_('new RegExp')).toBeUndefined();
|
|
874
|
+
it('Map/Set/RegExp in globals — new creates instances', () => {
|
|
875
|
+
// Map, Set, RegExp are now exposed as globals and whitelisted as safe constructors
|
|
876
|
+
expect(eval_('new Map')).toBeInstanceOf(Map);
|
|
877
|
+
expect(eval_('new Set')).toBeInstanceOf(Set);
|
|
878
|
+
expect(eval_('new RegExp')).toBeInstanceOf(RegExp);
|
|
880
879
|
});
|
|
881
880
|
|
|
882
|
-
it('new with args — parser
|
|
883
|
-
// Parser's parsePostfix consumes (args) as a call expression, so
|
|
884
|
-
// new Date(2024,0,1) becomes new(Date(2024,0,1)) — returns undefined
|
|
881
|
+
it('new with args — parser correctly passes args to constructor', () => {
|
|
885
882
|
const result = eval_('new Date(2024, 0, 1)');
|
|
886
|
-
expect(result).
|
|
883
|
+
expect(result).toBeInstanceOf(Date);
|
|
884
|
+
expect(result.getFullYear()).toBe(2024);
|
|
885
|
+
expect(result.getMonth()).toBe(0);
|
|
886
|
+
expect(result.getDate()).toBe(1);
|
|
887
887
|
});
|
|
888
888
|
|
|
889
889
|
it('blocks unsafe constructors', () => {
|
package/tests/utils.test.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import {
|
|
3
3
|
debounce, throttle, pipe, once, sleep,
|
|
4
|
-
escapeHtml, html, trust, uuid, camelCase, kebabCase,
|
|
4
|
+
escapeHtml, stripHtml, html, trust, uuid, camelCase, kebabCase,
|
|
5
5
|
deepClone, deepMerge, isEqual, param, parseQuery,
|
|
6
6
|
storage, session, bus,
|
|
7
|
+
// New utilities
|
|
8
|
+
range, unique, chunk, groupBy,
|
|
9
|
+
pick, omit, getPath, setPath, isEmpty,
|
|
10
|
+
capitalize, truncate,
|
|
11
|
+
clamp,
|
|
12
|
+
memoize,
|
|
13
|
+
retry, timeout,
|
|
7
14
|
} from '../src/utils.js';
|
|
8
15
|
|
|
9
16
|
|
|
@@ -147,6 +154,47 @@ describe('escapeHtml', () => {
|
|
|
147
154
|
});
|
|
148
155
|
|
|
149
156
|
|
|
157
|
+
describe('stripHtml', () => {
|
|
158
|
+
it('removes HTML tags from a string', () => {
|
|
159
|
+
expect(stripHtml('<p>Hello <b>World</b></p>')).toBe('Hello World');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('handles self-closing tags', () => {
|
|
163
|
+
expect(stripHtml('Line one<br/>Line two')).toBe('Line oneLine two');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('handles attributes inside tags', () => {
|
|
167
|
+
expect(stripHtml('<a href="https://example.com" class="link">click</a>')).toBe('click');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('strips nested tags', () => {
|
|
171
|
+
expect(stripHtml('<div><p><span>deep</span></p></div>')).toBe('deep');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('handles string with no tags', () => {
|
|
175
|
+
expect(stripHtml('no tags here')).toBe('no tags here');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('handles empty string', () => {
|
|
179
|
+
expect(stripHtml('')).toBe('');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('converts non-strings to string first', () => {
|
|
183
|
+
expect(stripHtml(42)).toBe('42');
|
|
184
|
+
expect(stripHtml(null)).toBe('null');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('preserves text content between multiple tags', () => {
|
|
188
|
+
expect(stripHtml('<li>one</li><li>two</li><li>three</li>')).toBe('onetwothree');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('strips angle-bracket patterns that look like tags', () => {
|
|
192
|
+
expect(stripHtml('a < b > c')).toBe('a c');
|
|
193
|
+
expect(stripHtml('5 > 3 and 2 < 4')).toBe('5 > 3 and 2 < 4');
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
|
|
150
198
|
describe('trust', () => {
|
|
151
199
|
it('returns a TrustedHTML instance', () => {
|
|
152
200
|
const t = trust('<b>bold</b>');
|
|
@@ -696,3 +744,497 @@ describe('storage — parse error fallback', () => {
|
|
|
696
744
|
localStorage.removeItem('bad');
|
|
697
745
|
});
|
|
698
746
|
});
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
// ===========================================================================
|
|
750
|
+
// NEW UTILITIES — Array
|
|
751
|
+
// ===========================================================================
|
|
752
|
+
|
|
753
|
+
describe('range', () => {
|
|
754
|
+
it('generates range from 0 to n-1 with single arg', () => {
|
|
755
|
+
expect(range(5)).toEqual([0, 1, 2, 3, 4]);
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
it('generates range from start to end-1', () => {
|
|
759
|
+
expect(range(2, 6)).toEqual([2, 3, 4, 5]);
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
it('generates range with custom step', () => {
|
|
763
|
+
expect(range(0, 10, 3)).toEqual([0, 3, 6, 9]);
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it('handles negative step (descending)', () => {
|
|
767
|
+
expect(range(5, 0, -1)).toEqual([5, 4, 3, 2, 1]);
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
it('returns empty array for zero or negative count', () => {
|
|
771
|
+
expect(range(0)).toEqual([]);
|
|
772
|
+
expect(range(-3)).toEqual([]);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
it('returns empty array when step goes wrong direction', () => {
|
|
776
|
+
expect(range(0, 5, -1)).toEqual([]);
|
|
777
|
+
expect(range(5, 0, 1)).toEqual([]);
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
it('handles step of 1 as default', () => {
|
|
781
|
+
expect(range(1, 4)).toEqual([1, 2, 3]);
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
it('handles float steps', () => {
|
|
785
|
+
const r = range(0, 1, 0.25);
|
|
786
|
+
expect(r.length).toBe(4);
|
|
787
|
+
expect(r[0]).toBeCloseTo(0);
|
|
788
|
+
expect(r[3]).toBeCloseTo(0.75);
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
describe('unique', () => {
|
|
794
|
+
it('deduplicates primitive arrays', () => {
|
|
795
|
+
expect(unique([1, 2, 2, 3, 1, 3])).toEqual([1, 2, 3]);
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
it('preserves order (first occurrence)', () => {
|
|
799
|
+
expect(unique([3, 1, 2, 1, 3])).toEqual([3, 1, 2]);
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
it('handles strings', () => {
|
|
803
|
+
expect(unique(['a', 'b', 'a', 'c'])).toEqual(['a', 'b', 'c']);
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
it('deduplicates by key function', () => {
|
|
807
|
+
const items = [{ id: 1, n: 'a' }, { id: 2, n: 'b' }, { id: 1, n: 'c' }];
|
|
808
|
+
const result = unique(items, item => item.id);
|
|
809
|
+
expect(result).toEqual([{ id: 1, n: 'a' }, { id: 2, n: 'b' }]);
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
it('handles empty array', () => {
|
|
813
|
+
expect(unique([])).toEqual([]);
|
|
814
|
+
});
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
describe('chunk', () => {
|
|
819
|
+
it('splits array into chunks of given size', () => {
|
|
820
|
+
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
it('returns single chunk when size >= length', () => {
|
|
824
|
+
expect(chunk([1, 2, 3], 5)).toEqual([[1, 2, 3]]);
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
it('handles exact division', () => {
|
|
828
|
+
expect(chunk([1, 2, 3, 4], 2)).toEqual([[1, 2], [3, 4]]);
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
it('handles chunk size of 1', () => {
|
|
832
|
+
expect(chunk([1, 2, 3], 1)).toEqual([[1], [2], [3]]);
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
it('returns empty array for empty input', () => {
|
|
836
|
+
expect(chunk([], 3)).toEqual([]);
|
|
837
|
+
});
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
describe('groupBy', () => {
|
|
842
|
+
it('groups by string key function', () => {
|
|
843
|
+
const items = [
|
|
844
|
+
{ type: 'fruit', name: 'apple' },
|
|
845
|
+
{ type: 'veg', name: 'carrot' },
|
|
846
|
+
{ type: 'fruit', name: 'banana' },
|
|
847
|
+
];
|
|
848
|
+
const result = groupBy(items, item => item.type);
|
|
849
|
+
expect(result).toEqual({
|
|
850
|
+
fruit: [{ type: 'fruit', name: 'apple' }, { type: 'fruit', name: 'banana' }],
|
|
851
|
+
veg: [{ type: 'veg', name: 'carrot' }],
|
|
852
|
+
});
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
it('groups by computed value', () => {
|
|
856
|
+
const nums = [1, 2, 3, 4, 5, 6];
|
|
857
|
+
const result = groupBy(nums, n => n % 2 === 0 ? 'even' : 'odd');
|
|
858
|
+
expect(result.even).toEqual([2, 4, 6]);
|
|
859
|
+
expect(result.odd).toEqual([1, 3, 5]);
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
it('handles empty array', () => {
|
|
863
|
+
expect(groupBy([], () => 'key')).toEqual({});
|
|
864
|
+
});
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
// ===========================================================================
|
|
869
|
+
// NEW UTILITIES — Object
|
|
870
|
+
// ===========================================================================
|
|
871
|
+
|
|
872
|
+
describe('pick', () => {
|
|
873
|
+
it('picks specified keys from object', () => {
|
|
874
|
+
const obj = { a: 1, b: 2, c: 3, d: 4 };
|
|
875
|
+
expect(pick(obj, ['a', 'c'])).toEqual({ a: 1, c: 3 });
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
it('ignores keys that do not exist', () => {
|
|
879
|
+
expect(pick({ a: 1 }, ['a', 'z'])).toEqual({ a: 1 });
|
|
880
|
+
});
|
|
881
|
+
|
|
882
|
+
it('returns empty object for empty keys', () => {
|
|
883
|
+
expect(pick({ a: 1 }, [])).toEqual({});
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
it('handles undefined/null values in picked keys', () => {
|
|
887
|
+
expect(pick({ a: null, b: undefined, c: 0 }, ['a', 'b', 'c'])).toEqual({ a: null, b: undefined, c: 0 });
|
|
888
|
+
});
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
describe('omit', () => {
|
|
893
|
+
it('omits specified keys from object', () => {
|
|
894
|
+
const obj = { a: 1, b: 2, c: 3, d: 4 };
|
|
895
|
+
expect(omit(obj, ['b', 'd'])).toEqual({ a: 1, c: 3 });
|
|
896
|
+
});
|
|
897
|
+
|
|
898
|
+
it('returns full object when no keys match', () => {
|
|
899
|
+
expect(omit({ a: 1, b: 2 }, ['z'])).toEqual({ a: 1, b: 2 });
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
it('returns empty object when all keys omitted', () => {
|
|
903
|
+
expect(omit({ a: 1, b: 2 }, ['a', 'b'])).toEqual({});
|
|
904
|
+
});
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
describe('getPath', () => {
|
|
909
|
+
it('gets nested value by dot path', () => {
|
|
910
|
+
const obj = { a: { b: { c: 42 } } };
|
|
911
|
+
expect(getPath(obj, 'a.b.c')).toBe(42);
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
it('gets top-level value', () => {
|
|
915
|
+
expect(getPath({ x: 10 }, 'x')).toBe(10);
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
it('returns fallback for missing path', () => {
|
|
919
|
+
expect(getPath({ a: 1 }, 'b.c', 'default')).toBe('default');
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
it('returns undefined by default for missing path', () => {
|
|
923
|
+
expect(getPath({}, 'a.b')).toBeUndefined();
|
|
924
|
+
});
|
|
925
|
+
|
|
926
|
+
it('handles null intermediate values', () => {
|
|
927
|
+
expect(getPath({ a: null }, 'a.b', 'nope')).toBe('nope');
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
it('works with array indices', () => {
|
|
931
|
+
const obj = { items: ['zero', 'one', 'two'] };
|
|
932
|
+
expect(getPath(obj, 'items.1')).toBe('one');
|
|
933
|
+
});
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
describe('setPath', () => {
|
|
938
|
+
it('sets nested value by dot path', () => {
|
|
939
|
+
const obj = { a: { b: { c: 1 } } };
|
|
940
|
+
setPath(obj, 'a.b.c', 99);
|
|
941
|
+
expect(obj.a.b.c).toBe(99);
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
it('creates intermediate objects when missing', () => {
|
|
945
|
+
const obj = {};
|
|
946
|
+
setPath(obj, 'a.b.c', 42);
|
|
947
|
+
expect(obj.a.b.c).toBe(42);
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
it('sets top-level value', () => {
|
|
951
|
+
const obj = {};
|
|
952
|
+
setPath(obj, 'x', 10);
|
|
953
|
+
expect(obj.x).toBe(10);
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
it('overwrites existing intermediate non-object', () => {
|
|
957
|
+
const obj = { a: 5 };
|
|
958
|
+
setPath(obj, 'a.b', 10);
|
|
959
|
+
expect(obj.a.b).toBe(10);
|
|
960
|
+
});
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
describe('isEmpty', () => {
|
|
965
|
+
it('returns true for null and undefined', () => {
|
|
966
|
+
expect(isEmpty(null)).toBe(true);
|
|
967
|
+
expect(isEmpty(undefined)).toBe(true);
|
|
968
|
+
});
|
|
969
|
+
|
|
970
|
+
it('returns true for empty string', () => {
|
|
971
|
+
expect(isEmpty('')).toBe(true);
|
|
972
|
+
});
|
|
973
|
+
|
|
974
|
+
it('returns true for empty array', () => {
|
|
975
|
+
expect(isEmpty([])).toBe(true);
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
it('returns true for empty object', () => {
|
|
979
|
+
expect(isEmpty({})).toBe(true);
|
|
980
|
+
});
|
|
981
|
+
|
|
982
|
+
it('returns false for non-empty string', () => {
|
|
983
|
+
expect(isEmpty('hello')).toBe(false);
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
it('returns false for non-empty array', () => {
|
|
987
|
+
expect(isEmpty([1])).toBe(false);
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
it('returns false for non-empty object', () => {
|
|
991
|
+
expect(isEmpty({ a: 1 })).toBe(false);
|
|
992
|
+
});
|
|
993
|
+
|
|
994
|
+
it('returns false for number zero', () => {
|
|
995
|
+
expect(isEmpty(0)).toBe(false);
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
it('returns false for boolean false', () => {
|
|
999
|
+
expect(isEmpty(false)).toBe(false);
|
|
1000
|
+
});
|
|
1001
|
+
|
|
1002
|
+
it('returns true for empty Map and Set', () => {
|
|
1003
|
+
expect(isEmpty(new Map())).toBe(true);
|
|
1004
|
+
expect(isEmpty(new Set())).toBe(true);
|
|
1005
|
+
});
|
|
1006
|
+
|
|
1007
|
+
it('returns false for non-empty Map and Set', () => {
|
|
1008
|
+
expect(isEmpty(new Map([['a', 1]]))).toBe(false);
|
|
1009
|
+
expect(isEmpty(new Set([1]))).toBe(false);
|
|
1010
|
+
});
|
|
1011
|
+
});
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
// ===========================================================================
|
|
1015
|
+
// NEW UTILITIES — String
|
|
1016
|
+
// ===========================================================================
|
|
1017
|
+
|
|
1018
|
+
describe('capitalize', () => {
|
|
1019
|
+
it('capitalizes first letter', () => {
|
|
1020
|
+
expect(capitalize('hello')).toBe('Hello');
|
|
1021
|
+
});
|
|
1022
|
+
|
|
1023
|
+
it('handles single character', () => {
|
|
1024
|
+
expect(capitalize('a')).toBe('A');
|
|
1025
|
+
});
|
|
1026
|
+
|
|
1027
|
+
it('handles empty string', () => {
|
|
1028
|
+
expect(capitalize('')).toBe('');
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
it('lowercases the rest', () => {
|
|
1032
|
+
expect(capitalize('hELLO')).toBe('Hello');
|
|
1033
|
+
});
|
|
1034
|
+
|
|
1035
|
+
it('handles already capitalized', () => {
|
|
1036
|
+
expect(capitalize('Hello')).toBe('Hello');
|
|
1037
|
+
});
|
|
1038
|
+
});
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
describe('truncate', () => {
|
|
1042
|
+
it('truncates long strings with ellipsis', () => {
|
|
1043
|
+
expect(truncate('Hello, World!', 8)).toBe('Hello, \u2026');
|
|
1044
|
+
});
|
|
1045
|
+
|
|
1046
|
+
it('does not truncate short strings', () => {
|
|
1047
|
+
expect(truncate('Hi', 10)).toBe('Hi');
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
it('uses custom suffix', () => {
|
|
1051
|
+
expect(truncate('Hello, World!', 8, '---')).toBe('Hello---');
|
|
1052
|
+
});
|
|
1053
|
+
|
|
1054
|
+
it('handles exact length (no truncation needed)', () => {
|
|
1055
|
+
expect(truncate('Hello', 5)).toBe('Hello');
|
|
1056
|
+
});
|
|
1057
|
+
|
|
1058
|
+
it('handles empty string', () => {
|
|
1059
|
+
expect(truncate('', 5)).toBe('');
|
|
1060
|
+
});
|
|
1061
|
+
|
|
1062
|
+
it('handles suffix longer than maxLen gracefully', () => {
|
|
1063
|
+
expect(truncate('Hello, World!', 2)).toBe('H\u2026');
|
|
1064
|
+
});
|
|
1065
|
+
});
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
// ===========================================================================
|
|
1069
|
+
// NEW UTILITIES — Number
|
|
1070
|
+
// ===========================================================================
|
|
1071
|
+
|
|
1072
|
+
describe('clamp', () => {
|
|
1073
|
+
it('clamps value below min to min', () => {
|
|
1074
|
+
expect(clamp(-5, 0, 100)).toBe(0);
|
|
1075
|
+
});
|
|
1076
|
+
|
|
1077
|
+
it('clamps value above max to max', () => {
|
|
1078
|
+
expect(clamp(150, 0, 100)).toBe(100);
|
|
1079
|
+
});
|
|
1080
|
+
|
|
1081
|
+
it('returns value when within range', () => {
|
|
1082
|
+
expect(clamp(50, 0, 100)).toBe(50);
|
|
1083
|
+
});
|
|
1084
|
+
|
|
1085
|
+
it('handles min === max', () => {
|
|
1086
|
+
expect(clamp(50, 10, 10)).toBe(10);
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
it('handles negative ranges', () => {
|
|
1090
|
+
expect(clamp(-50, -100, -10)).toBe(-50);
|
|
1091
|
+
expect(clamp(-200, -100, -10)).toBe(-100);
|
|
1092
|
+
});
|
|
1093
|
+
|
|
1094
|
+
it('clamps at boundaries', () => {
|
|
1095
|
+
expect(clamp(0, 0, 100)).toBe(0);
|
|
1096
|
+
expect(clamp(100, 0, 100)).toBe(100);
|
|
1097
|
+
});
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
// ===========================================================================
|
|
1102
|
+
// NEW UTILITIES — Function
|
|
1103
|
+
// ===========================================================================
|
|
1104
|
+
|
|
1105
|
+
describe('memoize', () => {
|
|
1106
|
+
it('caches results for same arguments', () => {
|
|
1107
|
+
const fn = vi.fn(x => x * 2);
|
|
1108
|
+
const memoized = memoize(fn);
|
|
1109
|
+
expect(memoized(5)).toBe(10);
|
|
1110
|
+
expect(memoized(5)).toBe(10);
|
|
1111
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
1112
|
+
});
|
|
1113
|
+
|
|
1114
|
+
it('recomputes for different arguments', () => {
|
|
1115
|
+
const fn = vi.fn(x => x * 2);
|
|
1116
|
+
const memoized = memoize(fn);
|
|
1117
|
+
expect(memoized(5)).toBe(10);
|
|
1118
|
+
expect(memoized(3)).toBe(6);
|
|
1119
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
1120
|
+
});
|
|
1121
|
+
|
|
1122
|
+
it('uses custom key function', () => {
|
|
1123
|
+
const fn = vi.fn((a, b) => a + b);
|
|
1124
|
+
const memoized = memoize(fn, (a, b) => `${a}:${b}`);
|
|
1125
|
+
expect(memoized(1, 2)).toBe(3);
|
|
1126
|
+
expect(memoized(1, 2)).toBe(3);
|
|
1127
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
1128
|
+
expect(memoized(2, 1)).toBe(3);
|
|
1129
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
1130
|
+
});
|
|
1131
|
+
|
|
1132
|
+
it('has .clear() to reset cache', () => {
|
|
1133
|
+
const fn = vi.fn(x => x * 2);
|
|
1134
|
+
const memoized = memoize(fn);
|
|
1135
|
+
memoized(5);
|
|
1136
|
+
memoized.clear();
|
|
1137
|
+
memoized(5);
|
|
1138
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
1139
|
+
});
|
|
1140
|
+
|
|
1141
|
+
it('respects maxSize option', () => {
|
|
1142
|
+
const fn = vi.fn(x => x * 2);
|
|
1143
|
+
const memoized = memoize(fn, { maxSize: 2 });
|
|
1144
|
+
memoized(1);
|
|
1145
|
+
memoized(2);
|
|
1146
|
+
memoized(3); // evicts 1
|
|
1147
|
+
expect(fn).toHaveBeenCalledTimes(3);
|
|
1148
|
+
memoized(2); // still cached
|
|
1149
|
+
expect(fn).toHaveBeenCalledTimes(3);
|
|
1150
|
+
memoized(1); // evicted, recomputes
|
|
1151
|
+
expect(fn).toHaveBeenCalledTimes(4);
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
// ===========================================================================
|
|
1157
|
+
// NEW UTILITIES — Async
|
|
1158
|
+
// ===========================================================================
|
|
1159
|
+
|
|
1160
|
+
describe('retry', () => {
|
|
1161
|
+
it('resolves on first success', async () => {
|
|
1162
|
+
const fn = vi.fn(async () => 42);
|
|
1163
|
+
const result = await retry(fn);
|
|
1164
|
+
expect(result).toBe(42);
|
|
1165
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
1166
|
+
});
|
|
1167
|
+
|
|
1168
|
+
it('retries on failure and succeeds', async () => {
|
|
1169
|
+
let calls = 0;
|
|
1170
|
+
const fn = async () => {
|
|
1171
|
+
calls++;
|
|
1172
|
+
if (calls < 3) throw new Error('fail');
|
|
1173
|
+
return 'ok';
|
|
1174
|
+
};
|
|
1175
|
+
const result = await retry(fn, { attempts: 3, delay: 0 });
|
|
1176
|
+
expect(result).toBe('ok');
|
|
1177
|
+
expect(calls).toBe(3);
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
it('throws after exhausting all attempts', async () => {
|
|
1181
|
+
const fn = async () => { throw new Error('always fails'); };
|
|
1182
|
+
await expect(retry(fn, { attempts: 3, delay: 0 })).rejects.toThrow('always fails');
|
|
1183
|
+
});
|
|
1184
|
+
|
|
1185
|
+
it('passes attempt number to function', async () => {
|
|
1186
|
+
const fn = vi.fn(async (attempt) => attempt);
|
|
1187
|
+
await retry(fn, { attempts: 1, delay: 0 });
|
|
1188
|
+
expect(fn).toHaveBeenCalledWith(1);
|
|
1189
|
+
});
|
|
1190
|
+
|
|
1191
|
+
it('uses exponential backoff when configured', async () => {
|
|
1192
|
+
vi.useFakeTimers();
|
|
1193
|
+
let calls = 0;
|
|
1194
|
+
const fn = async () => { calls++; if (calls < 3) throw new Error('fail'); return 'done'; };
|
|
1195
|
+
const p = retry(fn, { attempts: 3, delay: 100, backoff: 2 });
|
|
1196
|
+
// First call happens immediately, fails
|
|
1197
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
1198
|
+
// Second call after 100ms delay, fails
|
|
1199
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
1200
|
+
// Third call after 200ms delay (100 * 2), succeeds
|
|
1201
|
+
await vi.advanceTimersByTimeAsync(200);
|
|
1202
|
+
const result = await p;
|
|
1203
|
+
expect(result).toBe('done');
|
|
1204
|
+
vi.useRealTimers();
|
|
1205
|
+
});
|
|
1206
|
+
});
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
describe('timeout', () => {
|
|
1210
|
+
it('resolves if promise completes before timeout', async () => {
|
|
1211
|
+
const p = Promise.resolve(42);
|
|
1212
|
+
const result = await timeout(p, 1000);
|
|
1213
|
+
expect(result).toBe(42);
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
it('rejects if promise exceeds timeout', async () => {
|
|
1217
|
+
vi.useFakeTimers();
|
|
1218
|
+
const p = new Promise(() => {}); // never resolves
|
|
1219
|
+
const tp = timeout(p, 100);
|
|
1220
|
+
vi.advanceTimersByTime(100);
|
|
1221
|
+
await expect(tp).rejects.toThrow('Timed out');
|
|
1222
|
+
vi.useRealTimers();
|
|
1223
|
+
});
|
|
1224
|
+
|
|
1225
|
+
it('uses custom error message', async () => {
|
|
1226
|
+
vi.useFakeTimers();
|
|
1227
|
+
const p = new Promise(() => {});
|
|
1228
|
+
const tp = timeout(p, 100, 'Custom timeout');
|
|
1229
|
+
vi.advanceTimersByTime(100);
|
|
1230
|
+
await expect(tp).rejects.toThrow('Custom timeout');
|
|
1231
|
+
vi.useRealTimers();
|
|
1232
|
+
});
|
|
1233
|
+
|
|
1234
|
+
it('clears timer on successful resolution', async () => {
|
|
1235
|
+
const clearSpy = vi.spyOn(globalThis, 'clearTimeout');
|
|
1236
|
+
await timeout(Promise.resolve('ok'), 5000);
|
|
1237
|
+
expect(clearSpy).toHaveBeenCalled();
|
|
1238
|
+
clearSpy.mockRestore();
|
|
1239
|
+
});
|
|
1240
|
+
});
|