-
-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathReadonlyRecord.ts
2330 lines (2241 loc) · 68.6 KB
/
ReadonlyRecord.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The `ReadonlyRecord.ts` module enables dealing in a functional way with
* Typescript's `Readonly<Record<K, T>>` type. That is similar to the
* `Record.ts` module, but for a record with all properties
* declared as `readonly`.
*
* @since 2.5.0
*/
import { Applicative, Applicative1, Applicative2, Applicative2C, Applicative3, Applicative3C } from './Applicative'
import { Compactable1 } from './Compactable'
import { Either } from './Either'
import { Eq, fromEquals } from './Eq'
import { Filterable1 } from './Filterable'
import { FilterableWithIndex1, PredicateWithIndex, RefinementWithIndex } from './FilterableWithIndex'
import { Foldable as FoldableHKT, Foldable1, Foldable2, Foldable3 } from './Foldable'
import { FoldableWithIndex1 } from './FoldableWithIndex'
import { flow, identity, pipe, SK } from './function'
import { flap as flap_, Functor1 } from './Functor'
import { FunctorWithIndex1 } from './FunctorWithIndex'
import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from './HKT'
import * as _ from './internal'
import { Magma } from './Magma'
import { Monoid } from './Monoid'
import { Option } from './Option'
import { Ord } from './Ord'
import { Predicate } from './Predicate'
import { Refinement } from './Refinement'
import { Semigroup } from './Semigroup'
import { Separated, separated } from './Separated'
import { Show } from './Show'
import * as S from './string'
import { Traversable1 } from './Traversable'
import { TraversableWithIndex1 } from './TraversableWithIndex'
import { Unfoldable, Unfoldable1 } from './Unfoldable'
import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault } from './Witherable'
// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------
/**
* @category model
* @since 2.5.0
*/
export type ReadonlyRecord<K extends string, T> = Readonly<Record<K, T>>
// -------------------------------------------------------------------------------------
// interop
// -------------------------------------------------------------------------------------
/**
* Builds a `ReadonlyRecord` by copying a `Record`.
*
* @example
* import { ReadonlyRecord, fromRecord } from "fp-ts/ReadonlyRecord"
*
* const x: Record<string, number> = { a: 1, b: 2 };
* const y: ReadonlyRecord<string, number> = fromRecord(x);
* assert.deepStrictEqual(x,y);
* // `y.a = 5` gives compiler error
*
* @category interop
* @since 2.5.0
*/
export const fromRecord = <K extends string, A>(r: Record<K, A>): ReadonlyRecord<K, A> => Object.assign({}, r)
/**
* Builds a mutable `Record` from a `ReadonlyRecord`.
*
* @example
* import { ReadonlyRecord, toRecord } from "fp-ts/ReadonlyRecord"
*
* const x: ReadonlyRecord<string, number> = { a: 1, b: 2 };
* const y: Record<string, number> = toRecord(x);
* assert.deepStrictEqual(x,y);
* y.a = 5; // it's ok, y is mutable
*
* @category interop
* @since 2.5.0
*/
export const toRecord = <K extends string, A>(r: ReadonlyRecord<K, A>): Record<K, A> => Object.assign({}, r)
/**
* Calculate the number of key/value pairs in a `ReadonlyRecord`,
*
* @example
* import { size } from "fp-ts/ReadonlyRecord";
*
* assert.deepStrictEqual(size({ a: true, b: 2, c: "three" }), 3);
*
* @since 2.5.0
*/
export const size = <A>(r: ReadonlyRecord<string, A>): number => Object.keys(r).length
/**
* Test whether a `ReadonlyRecord` is empty.
*
* @example
* import { isEmpty } from "fp-ts/ReadonlyRecord"
*
* assert.deepStrictEqual(isEmpty({}), true);
* assert.deepStrictEqual(isEmpty({ a: 3 }), false);
* @since 2.5.0
*/
export const isEmpty = <A>(r: ReadonlyRecord<string, A>): boolean => {
for (const k in r) {
if (_.has.call(r, k)) {
return false
}
}
return true
}
const keys_ = (O: Ord<string>) => <K extends string>(r: ReadonlyRecord<K, unknown>): ReadonlyArray<K> =>
(Object.keys(r) as any).sort(O.compare)
/**
* @since 2.5.0
*/
export const keys: <K extends string>(r: ReadonlyRecord<K, unknown>) => ReadonlyArray<K> =
/*#__PURE__*/
keys_(S.Ord)
/**
* Map a `ReadonlyRecord` into an `ReadonlyArray`.
*
* @example
* import { collect } from 'fp-ts/ReadonlyRecord'
* import { Ord } from 'fp-ts/string'
*
* const f = <A>(k: string, a: A) => `${k.toUpperCase()}-${a}`;
* const x = { c: 3, a: "foo", b: false };
* assert.deepStrictEqual(collect(Ord)(f)(x), ["A-foo", "B-false", "C-3"]);
*
* @since 2.5.0
*/
export function collect(
O: Ord<string>
): <K extends string, A, B>(f: (k: K, a: A) => B) => (r: ReadonlyRecord<K, A>) => ReadonlyArray<B>
/**
* Use the overload constrained by `Ord` instead.
*
* @deprecated
*/
export function collect<K extends string, A, B>(f: (k: K, a: A) => B): (r: ReadonlyRecord<K, A>) => ReadonlyArray<B>
export function collect<A, B>(
O: Ord<string> | ((k: string, a: A) => B)
):
| (<K extends string, A, B>(f: (k: K, a: A) => B) => (r: ReadonlyRecord<K, A>) => ReadonlyArray<B>)
| ((r: ReadonlyRecord<string, A>) => ReadonlyArray<B>) {
if (typeof O === 'function') {
return collect(S.Ord)(O)
}
const keysO = keys_(O)
return <K extends string, A, B>(f: (k: K, a: A) => B) => (r: ReadonlyRecord<K, A>) => {
const out: Array<B> = []
for (const key of keysO(r)) {
out.push(f(key, r[key]))
}
return out
}
}
/**
* Get a sorted `ReadonlyArray` of the key/value pairs contained in a `ReadonlyRecord`.
*
* @example
* import { toReadonlyArray } from 'fp-ts/ReadonlyRecord'
*
* const x = { c: 3, a: "foo", b: false };
* assert.deepStrictEqual(toReadonlyArray(x), [
* ["a", "foo"],
* ["b", false],
* ["c", 3],
* ]);
*
* @since 2.5.0
*/
export const toReadonlyArray: <K extends string, A>(r: ReadonlyRecord<K, A>) => ReadonlyArray<readonly [K, A]> =
/*#__PURE__*/
collect(S.Ord)((k, a) => [k, a])
/**
* Unfolds a `ReadonlyRecord` into a list of key/value pairs.
*
* Given an `Unfoldable` class type `U` such as `array` or `readonlyArray`,
* it uses the `unfold` function to create an instance of `U`,
* providing an iterating function that iterates over each
* key/value pair in the record sorted alphabetically by key.
*
* @example
* import { array, readonlyArray } from 'fp-ts'
* import { toUnfoldable } from 'fp-ts/ReadonlyRecord'
*
* assert.deepStrictEqual(toUnfoldable(array)({ b: 2, a: 1 }),[ [ 'a', 1 ], [ 'b', 2 ]])
* assert.deepStrictEqual(toUnfoldable(readonlyArray)({ b: 2, a: 1 }),[ [ 'a', 1 ], [ 'b', 2 ]])
*
* @category destructors
* @since 2.5.0
*/
export function toUnfoldable<F extends URIS>(
U: Unfoldable1<F>
): <K extends string, A>(r: ReadonlyRecord<K, A>) => Kind<F, readonly [K, A]>
export function toUnfoldable<F>(
U: Unfoldable<F>
): <K extends string, A>(r: ReadonlyRecord<K, A>) => HKT<F, readonly [K, A]>
export function toUnfoldable<F>(U: Unfoldable<F>): <A>(r: ReadonlyRecord<string, A>) => HKT<F, readonly [string, A]> {
return (r) => {
const sas = toReadonlyArray(r)
const len = sas.length
return U.unfold(0, (b) => (b < len ? _.some([sas[b], b + 1]) : _.none))
}
}
/**
* Insert or replace a key/value pair in a `ReadonlyRecord`.
*
* @example
* import { upsertAt } from 'fp-ts/ReadonlyRecord'
*
* assert.deepStrictEqual(upsertAt("a", 5)({ a: 1, b: 2 }), { a: 5, b: 2 });
* assert.deepStrictEqual(upsertAt("c", 5)({ a: 1, b: 2 }), { a: 1, b: 2, c: 5 });
*
* @category combinators
* @since 2.10.0
*/
export const upsertAt = <A>(k: string, a: A) => (r: ReadonlyRecord<string, A>): ReadonlyRecord<string, A> => {
if (_.has.call(r, k) && r[k] === a) {
return r
}
const out: Record<string, A> = Object.assign({}, r)
out[k] = a
return out
}
/**
* Test whether or not a key exists in a `ReadonlyRecord`.
*
* Note. This function is not pipeable because is a `Refinement`.
*
* @example
* import { has } from 'fp-ts/ReadonlyRecord'
*
* assert.deepStrictEqual(has("a", { a: 1, b: 2 }), true);
* assert.deepStrictEqual(has("c", { a: 1, b: 2 }), false);
*
* @since 2.10.0
*/
export const has = <K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K => _.has.call(r, k)
/**
* Delete a key and value from a `ReadonlyRecord`.
*
* @example
* import { deleteAt } from 'fp-ts/ReadonlyRecord'
*
* assert.deepStrictEqual(deleteAt("a")({ a: 1, b: 2 }), { b: 2 });
* assert.deepStrictEqual(deleteAt("c")({ a: 1, b: 2 }), { a: 1, b: 2 });
*
* @category combinators
* @since 2.5.0
*/
export function deleteAt<K extends string>(
k: K
): <KS extends string, A>(r: ReadonlyRecord<KS, A>) => ReadonlyRecord<string extends K ? string : Exclude<KS, K>, A>
export function deleteAt(k: string): <A>(r: ReadonlyRecord<string, A>) => ReadonlyRecord<string, A> {
return <A>(r: ReadonlyRecord<string, A>) => {
if (!_.has.call(r, k)) {
return r
}
const out: Record<string, A> = Object.assign({}, r)
delete out[k]
return out
}
}
/**
* Replace a key/value pair in a `ReadonlyRecord`.
*
* @returns If the specified key exists it returns an `Option` containing a new `Record`
* with the entry updated, otherwise it returns `None`
*
* @example
* import { updateAt } from 'fp-ts/ReadonlyRecord'
* import { option } from 'fp-ts'
*
* assert.deepStrictEqual(updateAt("a", 3)({ a: 1, b: 2 }), option.some({ a: 3, b: 2 }));
* assert.deepStrictEqual(updateAt("c", 3)({ a: 1, b: 2 }), option.none);
*
* @since 2.5.0
*/
export const updateAt = <A>(k: string, a: A) => <K extends string>(
r: ReadonlyRecord<K, A>
): Option<ReadonlyRecord<K, A>> => {
if (!has(k, r)) {
return _.none
}
if (r[k] === a) {
return _.some(r)
}
const out: Record<K, A> = Object.assign({}, r)
out[k] = a
return _.some(out)
}
/**
* Applies a mapping function to one specific key/value pair in a `ReadonlyRecord`.
*
* @returns If the specified key exists it returns an `Option` containing a new `Record`
* with the entry updated, otherwise it returns `None`
*
* @example
* import { modifyAt } from 'fp-ts/ReadonlyRecord'
* import { option } from 'fp-ts'
*
* assert.deepStrictEqual(modifyAt("a", (x: number) => x * 3)({ a: 1, b: 2 }), option.some({ a: 3, b: 2 }));
* assert.deepStrictEqual(modifyAt("c", (x: number) => x * 3)({ a: 1, b: 2 }), option.none);
*
* @since 2.5.0
*/
export const modifyAt = <A>(k: string, f: (a: A) => A) => <K extends string>(
r: ReadonlyRecord<K, A>
): Option<ReadonlyRecord<K, A>> => {
if (!has(k, r)) {
return _.none
}
const next = f(r[k])
if (next === r[k]) {
return _.some(r)
}
const out: Record<K, A> = Object.assign({}, r)
out[k] = next
return _.some(out)
}
/**
* Delete a key and value from a `ReadonlyRecord`, returning the value as well as the subsequent `ReadonlyRecord`.
*
* @returns If the specified key exists it returns an `Option` containing a new `ReadonlyRecord`
* with the entry removed, otherwise it returns `None`
*
* @example
* import { pop } from 'fp-ts/ReadonlyRecord'
* import { option } from 'fp-ts'
*
* assert.deepStrictEqual(pop("a")({ a: 1, b: 2, c: 3 }), option.some([1, { b: 2, c: 3 }]));
* assert.deepStrictEqual(pop("x")({ a: 1, b: 2, c: 3 }), option.none);
*
* @since 2.5.0
*/
export function pop<K extends string>(
k: K
): <KS extends string, A>(
r: ReadonlyRecord<KS, A>
) => Option<readonly [A, ReadonlyRecord<string extends K ? string : Exclude<KS, K>, A>]>
export function pop(k: string): <A>(r: ReadonlyRecord<string, A>) => Option<readonly [A, ReadonlyRecord<string, A>]> {
const deleteAtk = deleteAt(k)
return (r) => {
const oa = lookup(k, r)
return _.isNone(oa) ? _.none : _.some([oa.value, deleteAtk(r)])
}
}
// TODO: remove non-curried overloading in v3
/**
* Test whether one `ReadonlyRecord` contains all of the keys and values
* contained in another `ReadonlyRecord`.
*
* @example
* import { isSubrecord } from 'fp-ts/ReadonlyRecord'
* import { string } from 'fp-ts'
*
* assert.deepStrictEqual(
* isSubrecord(string.Eq)({ a: "foo", b: "bar", c: "baz" })({ a: "foo", b: "bar", c: "baz" }),
* true
* );
* assert.deepStrictEqual(
* isSubrecord(string.Eq)({ a: "foo", b: "bar", c: "baz" })({ a: "foo", c: "baz" }),
* true
* );
* assert.deepStrictEqual(
* isSubrecord(string.Eq)({ a: "foo", b: "bar", c: "baz" })({ a: "foo", b: "not-bar", c: "baz" }),
* false
* );
* assert.deepStrictEqual(
* isSubrecord(string.Eq)({ a: "foo", b: "bar" })({ a: "foo", b: "bar", c: "baz" }),
* false
* );
*
* @since 2.5.0
*/
export function isSubrecord<A>(
E: Eq<A>
): {
(that: ReadonlyRecord<string, A>): (me: ReadonlyRecord<string, A>) => boolean
(me: ReadonlyRecord<string, A>, that: ReadonlyRecord<string, A>): boolean
}
export function isSubrecord<A>(
E: Eq<A>
): (
me: ReadonlyRecord<string, A>,
that?: ReadonlyRecord<string, A>
) => boolean | ((me: ReadonlyRecord<string, A>) => boolean) {
return (me, that?) => {
if (that === undefined) {
const isSubrecordE = isSubrecord(E)
return (that) => isSubrecordE(that, me)
}
for (const k in me) {
if (!_.has.call(that, k) || !E.equals(me[k], that[k])) {
return false
}
}
return true
}
}
// TODO: remove non-curried overloading in v3
/**
* Lookup the value for a key in a `ReadonlyRecord`.
*
* @returns If the specified key exists it returns an `Option` containing the value,
* otherwise it returns `None`
*
* @example
* import { lookup } from 'fp-ts/ReadonlyRecord'
* import { option } from 'fp-ts'
*
* assert.deepStrictEqual(lookup("b")({ a: "foo", b: "bar" }), option.some("bar"));
* assert.deepStrictEqual(lookup("c")({ a: "foo", b: "bar" }), option.none);
*
* @since 2.5.0
*/
export function lookup(k: string): <A>(r: ReadonlyRecord<string, A>) => Option<A>
export function lookup<A>(k: string, r: ReadonlyRecord<string, A>): Option<A>
export function lookup<A>(
k: string,
r?: ReadonlyRecord<string, A>
): Option<A> | ((r: ReadonlyRecord<string, A>) => Option<A>) {
if (r === undefined) {
return (r) => lookup(k, r)
}
return _.has.call(r, k) ? _.some(r[k]) : _.none
}
/**
* @since 2.5.0
*/
export const empty: ReadonlyRecord<string, never> = {}
/**
* Map a `ReadonlyRecord` passing the keys to the iterating function.
*
* @example
* import { mapWithIndex } from "fp-ts/ReadonlyRecord";
*
* const f = (k: string, n: number) => `${k.toUpperCase()}-${n}`;
* assert.deepStrictEqual(mapWithIndex(f)({ a: 3, b: 5 }), { a: "A-3", b: "B-5" });
*
* @category combinators
* @since 2.5.0
*/
export function mapWithIndex<K extends string, A, B>(
f: (k: K, a: A) => B
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<K, B>
export function mapWithIndex<A, B>(
f: (k: string, a: A) => B
): (fa: ReadonlyRecord<string, A>) => ReadonlyRecord<string, B> {
return (r) => {
const out: Record<string, B> = {}
for (const k in r) {
if (_.has.call(r, k)) {
out[k] = f(k, r[k])
}
}
return out
}
}
/**
* Map a `ReadonlyRecord` passing the values to the iterating function.
*
* @example
* import { map } from "fp-ts/ReadonlyRecord";
*
* const f = (n: number) => `-${n}-`;
* assert.deepStrictEqual(map(f)({ a: 3, b: 5 }), { a: "-3-", b: "-5-" });
*
* @category combinators
* @since 2.5.0
*/
export function map<A, B>(f: (a: A) => B): <K extends string>(fa: ReadonlyRecord<K, A>) => ReadonlyRecord<K, B>
export function map<A, B>(f: (a: A) => B): (fa: ReadonlyRecord<string, A>) => ReadonlyRecord<string, B> {
return mapWithIndex((_, a) => f(a))
}
/**
* Reduces a `ReadonlyRecord` passing each key/value pair to the iterating function.
* Entries are processed in the order, sorted by key according to
* the given `Ord`.
*
* @example
* import { reduceWithIndex } from "fp-ts/ReadonlyRecord";
* import { Ord } from "fp-ts/string";
*
* const x = { c: 3, a: "foo", b: false };
* assert.deepStrictEqual(reduceWithIndex(Ord)([] as string[], (k, b, a) => [...b, `${k}-${a}`])(x), [
* "a-foo",
* "b-false",
* "c-3",
* ]);
*
* @since 2.5.0
*/
export function reduceWithIndex(
O: Ord<string>
): <K extends string, A, B>(b: B, f: (k: K, b: B, a: A) => B) => (fa: ReadonlyRecord<K, A>) => B
/**
* Use the overload constrained by `Ord` instead.
*
* @deprecated
*/
export function reduceWithIndex<K extends string, A, B>(
b: B,
f: (k: K, b: B, a: A) => B
): (fa: ReadonlyRecord<K, A>) => B
export function reduceWithIndex<A, B>(
...args: [Ord<string>] | [B, (k: string, b: B, a: A) => B]
):
| ((b: B, f: (k: string, b: B, a: A) => B) => (fa: ReadonlyRecord<string, A>) => B)
| ((fa: ReadonlyRecord<string, A>) => B) {
if (args.length === 2) {
return reduceWithIndex(S.Ord)(...args)
}
const keysO = keys_(args[0])
return (b, f) => (fa) => {
let out: B = b
const ks = keysO(fa)
const len = ks.length
for (let i = 0; i < len; i++) {
const k = ks[i]
out = f(k, out, fa[k])
}
return out
}
}
/**
* Map and fold a `ReadonlyRecord`.
* Map the `ReadonlyRecord` passing each key/value pair to the iterating function.
* Then fold the results using the provided `Monoid`.
*
* @example
* import { foldMapWithIndex } from "fp-ts/ReadonlyRecord";
* import { Ord } from "fp-ts/string";
* import { Monoid } from "fp-ts/Monoid";
*
* const m: Monoid<string> = { empty: "", concat: (x: string, y: string) => (x ? `${x} -> ${y}` : `${y}`) };
* const f = (k:string, a: number) => `${k}-${a}`
* const x = { c: 3, a: 1, b: 2 };
* assert.deepStrictEqual(foldMapWithIndex(Ord)(m)(f)(x), "a-1 -> b-2 -> c-3");
*
* @since 2.5.0
*/
export function foldMapWithIndex(
O: Ord<string>
): <M>(M: Monoid<M>) => <K extends string, A>(f: (k: K, a: A) => M) => (fa: ReadonlyRecord<K, A>) => M
/**
* Use the overload constrained by `Ord` instead.
*
* @deprecated
*/
export function foldMapWithIndex<M>(
M: Monoid<M>
): <K extends string, A>(f: (k: K, a: A) => M) => (fa: ReadonlyRecord<K, A>) => M
export function foldMapWithIndex<M>(
O: Ord<string> | Monoid<M>
):
| ((M: Monoid<M>) => <A>(f: (k: string, a: A) => M) => (fa: ReadonlyRecord<string, A>) => M)
| (<A>(f: (k: string, a: A) => M) => (fa: ReadonlyRecord<string, A>) => M) {
if ('compare' in O) {
const keysO = keys_(O)
return (M: Monoid<M>) => <A>(f: (k: string, a: A) => M) => (fa: ReadonlyRecord<string, A>) => {
let out: M = M.empty
const ks = keysO(fa)
const len = ks.length
for (let i = 0; i < len; i++) {
const k = ks[i]
out = M.concat(out, f(k, fa[k]))
}
return out
}
}
return foldMapWithIndex(S.Ord)(O)
}
/**
* Same as `reduceWithIndex`, but reduce starting from the right
* (i.e. in reverse order, from the last to the first entry according to
* the given `Ord`).
*
* @example
* import { reduceRightWithIndex } from "fp-ts/ReadonlyRecord";
* import { Ord } from "fp-ts/string";
*
* const x = { c: 3, a: "foo", b: false };
* assert.deepStrictEqual(reduceRightWithIndex(Ord)([] as string[], (k, a, b) => [...b, `${k}-${a}`])(x), [
* "c-3",
* "b-false",
* "a-foo",
* ]);
*
* @since 2.5.0
*/
export function reduceRightWithIndex(
O: Ord<string>
): <K extends string, A, B>(b: B, f: (k: K, a: A, b: B) => B) => (fa: ReadonlyRecord<K, A>) => B
/**
* Use the overload constrained by `Ord` instead.
*
* @deprecated
*/
export function reduceRightWithIndex<K extends string, A, B>(
b: B,
f: (k: K, a: A, b: B) => B
): (fa: ReadonlyRecord<K, A>) => B
export function reduceRightWithIndex<A, B>(
...args: [Ord<string>] | [B, (k: string, a: A, b: B) => B]
):
| ((b: B, f: (k: string, a: A, b: B) => B) => (fa: ReadonlyRecord<string, A>) => B)
| ((fa: ReadonlyRecord<string, A>) => B) {
if (args.length === 2) {
return reduceRightWithIndex(S.Ord)(...args)
}
const keysO = keys_(args[0])
return (b, f) => (fa) => {
let out: B = b
const ks = keysO(fa)
const len = ks.length
for (let i = len - 1; i >= 0; i--) {
const k = ks[i]
out = f(k, fa[k], out)
}
return out
}
}
/**
* Create a `ReadonlyRecord` with one key/value pair.
*
* @example
* import { singleton } from "fp-ts/ReadonlyRecord";
*
* assert.deepStrictEqual(singleton("a", 1), { a: 1 });
*
* @category constructors
* @since 2.5.0
*/
export const singleton = <A>(k: string, a: A): ReadonlyRecord<string, A> => ({ [k]: a })
/**
* @since 2.5.0
*/
export function traverseWithIndex<F extends URIS3>(
F: Applicative3<F>
): <K extends string, R, E, A, B>(
f: (k: K, a: A) => Kind3<F, R, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export function traverseWithIndex<F extends URIS3, E>(
F: Applicative3C<F, E>
): <K extends string, R, A, B>(
f: (k: K, a: A) => Kind3<F, R, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export function traverseWithIndex<F extends URIS2>(
F: Applicative2<F>
): <K extends string, E, A, B>(
f: (k: K, a: A) => Kind2<F, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export function traverseWithIndex<F extends URIS2, E>(
F: Applicative2C<F, E>
): <K extends string, A, B>(
f: (k: K, a: A) => Kind2<F, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export function traverseWithIndex<F extends URIS>(
F: Applicative1<F>
): <K extends string, A, B>(
f: (k: K, a: A) => Kind<F, B>
) => (ta: ReadonlyRecord<K, A>) => Kind<F, ReadonlyRecord<K, B>>
export function traverseWithIndex<F>(
F: Applicative<F>
): <K extends string, A, B>(f: (k: K, a: A) => HKT<F, B>) => (ta: ReadonlyRecord<K, A>) => HKT<F, ReadonlyRecord<K, B>>
export function traverseWithIndex<F>(
F: Applicative<F>
): <A, B>(f: (k: string, a: A) => HKT<F, B>) => (ta: ReadonlyRecord<string, A>) => HKT<F, ReadonlyRecord<string, B>> {
const traverseWithIndexOF = _traverseWithIndex(S.Ord)(F)
return (f) => (ta) => traverseWithIndexOF(ta, f)
}
/**
* @since 2.5.0
*/
export function traverse<F extends URIS3>(
F: Applicative3<F>
): <R, E, A, B>(
f: (a: A) => Kind3<F, R, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export function traverse<F extends URIS3, E>(
F: Applicative3C<F, E>
): <R, A, B>(
f: (a: A) => Kind3<F, R, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export function traverse<F extends URIS2>(
F: Applicative2<F>
): <E, A, B>(
f: (a: A) => Kind2<F, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export function traverse<F extends URIS2, E>(
F: Applicative2C<F, E>
): <A, B>(
f: (a: A) => Kind2<F, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export function traverse<F extends URIS>(
F: Applicative1<F>
): <A, B>(f: (a: A) => Kind<F, B>) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind<F, ReadonlyRecord<K, B>>
export function traverse<F>(
F: Applicative<F>
): <A, B>(f: (a: A) => HKT<F, B>) => <K extends string>(ta: ReadonlyRecord<K, A>) => HKT<F, ReadonlyRecord<K, B>>
export function traverse<F>(
F: Applicative<F>
): <A, B>(f: (a: A) => HKT<F, B>) => (ta: ReadonlyRecord<string, A>) => HKT<F, ReadonlyRecord<string, B>> {
const traverseOF = _traverse(S.Ord)(F)
return (f) => (ta) => traverseOF(ta, f)
}
/**
* `ReadonlyRecord` sequencing,
* i.e., take a `ReadonlyRecord` in which elements are monads
* and return a monad of a `ReadonlyRecord` of the base types.
* The following example for instance shows sequencing
* a `ReadonlyRecord<string, Option<number>>`
* into an `Option<ReadonlyRecord<string, number>>`.
*
* `sequence` in `ReadonlyRecord` is equivalent to `sequenceS` in `Apply.ts`.
*
* @example
* import { sequence } from "fp-ts/ReadonlyRecord";
* import { option } from "fp-ts";
* import { sequenceS } from "fp-ts/Apply";
*
* assert.deepStrictEqual(
* sequence(option.Applicative)({ a: option.some(1), b: option.some(2) }),
* option.some({ a: 1, b: 2 })
* );
* assert.deepStrictEqual(sequence(option.Applicative)({ a: option.some(1), b: option.none }), option.none);
* assert.deepStrictEqual(
* sequence(option.Applicative)({ a: option.some(1), b: option.some(2) }),
* sequenceS(option.Applicative)({ a: option.some(1), b: option.some(2) })
* );
*
* @since 2.5.0
*/
export function sequence<F extends URIS3>(
F: Applicative3<F>
): <K extends string, R, E, A>(ta: ReadonlyRecord<K, Kind3<F, R, E, A>>) => Kind3<F, R, E, ReadonlyRecord<K, A>>
export function sequence<F extends URIS3, E>(
F: Applicative3C<F, E>
): <K extends string, R, A>(ta: ReadonlyRecord<K, Kind3<F, R, E, A>>) => Kind3<F, R, E, ReadonlyRecord<K, A>>
export function sequence<F extends URIS2>(
F: Applicative2<F>
): <K extends string, E, A>(ta: ReadonlyRecord<K, Kind2<F, E, A>>) => Kind2<F, E, ReadonlyRecord<K, A>>
export function sequence<F extends URIS2, E>(
F: Applicative2C<F, E>
): <K extends string, A>(ta: ReadonlyRecord<K, Kind2<F, E, A>>) => Kind2<F, E, ReadonlyRecord<K, A>>
export function sequence<F extends URIS>(
F: Applicative1<F>
): <K extends string, A>(ta: ReadonlyRecord<K, Kind<F, A>>) => Kind<F, ReadonlyRecord<K, A>>
export function sequence<F>(
F: Applicative<F>
): <K extends string, A>(ta: ReadonlyRecord<K, HKT<F, A>>) => HKT<F, ReadonlyRecord<K, A>>
export function sequence<F>(
F: Applicative<F>
): <A>(ta: ReadonlyRecord<string, HKT<F, A>>) => HKT<F, ReadonlyRecord<string, A>> {
return _sequence(S.Ord)(F)
}
/**
* @category Witherable
* @since 2.6.5
*/
export const wither: PipeableWither1<URI> = <F>(
F: Applicative<F>
): (<A, B>(f: (a: A) => HKT<F, Option<B>>) => (fa: ReadonlyRecord<string, A>) => HKT<F, ReadonlyRecord<string, B>>) => {
const traverseF = traverse(F)
return (f) => (fa) => F.map(pipe(fa, traverseF(f)), compact)
}
/**
* @category Witherable
* @since 2.6.5
*/
export const wilt: PipeableWilt1<URI> = <F>(
F: Applicative<F>
): (<A, B, C>(
f: (a: A) => HKT<F, Either<B, C>>
) => (fa: ReadonlyRecord<string, A>) => HKT<F, Separated<ReadonlyRecord<string, B>, ReadonlyRecord<string, C>>>) => {
const traverseF = traverse(F)
return (f) => (fa) => F.map(pipe(fa, traverseF(f)), separate)
}
/**
* Maps a `ReadonlyRecord` with a function returning an `Either` and
* partitions the resulting `ReadonlyRecord` into `Left`s and `Right`s.
*
* @example
* import { partitionMapWithIndex } from "fp-ts/ReadonlyRecord"
* import { either } from "fp-ts"
*
* const f = (key: string, a: number) =>
* a >= 0 ? either.right(`${key} is >= 0 (${a})`) : either.left(`${key} is < 0 (${a})`);
* assert.deepStrictEqual(partitionMapWithIndex(f)({ a: -1, b: 2, c: 123 }), {
* left: {
* a: "a is < 0 (-1)",
* },
* right: {
* b: "b is >= 0 (2)",
* c: "c is >= 0 (123)",
* },
* });
*
* @since 2.5.0
*/
export function partitionMapWithIndex<K extends string, A, B, C>(
f: (key: K, a: A) => Either<B, C>
): (fa: ReadonlyRecord<K, A>) => Separated<ReadonlyRecord<string, B>, ReadonlyRecord<string, C>>
export function partitionMapWithIndex<A, B, C>(
f: (key: string, a: A) => Either<B, C>
): (fa: ReadonlyRecord<string, A>) => Separated<ReadonlyRecord<string, B>, ReadonlyRecord<string, C>> {
return (r) => {
const left: Record<string, B> = {}
const right: Record<string, C> = {}
for (const k in r) {
if (_.has.call(r, k)) {
const e = f(k, r[k])
switch (e._tag) {
case 'Left':
left[k] = e.left
break
case 'Right':
right[k] = e.right
break
}
}
}
return separated(left, right)
}
}
/**
* Partition a `ReadonlyRecord` into two parts according to a predicate
* that takes a key and a value.
*
* @example
* import { partitionWithIndex } from "fp-ts/ReadonlyRecord"
*
* assert.deepStrictEqual(
* partitionWithIndex((key: string, a: number) => key.length <= 1 && a > 0)({ a: -1, b: 2, ccc: 7 }),
* {
* left: {
* a: -1,
* ccc: 7,
* },
* right: {
* b: 2,
* },
* }
* );
*
* @since 2.5.0
*/
export function partitionWithIndex<K extends string, A, B extends A>(
refinementWithIndex: RefinementWithIndex<K, A, B>
): (fa: ReadonlyRecord<K, A>) => Separated<ReadonlyRecord<string, A>, ReadonlyRecord<string, B>>
export function partitionWithIndex<K extends string, A>(
predicateWithIndex: PredicateWithIndex<K, A>
): <B extends A>(fb: ReadonlyRecord<K, B>) => Separated<ReadonlyRecord<string, B>, ReadonlyRecord<string, B>>
export function partitionWithIndex<K extends string, A>(
predicateWithIndex: PredicateWithIndex<K, A>
): (fa: ReadonlyRecord<K, A>) => Separated<ReadonlyRecord<string, A>, ReadonlyRecord<string, A>>
export function partitionWithIndex<A>(
predicateWithIndex: PredicateWithIndex<string, A>
): (fa: ReadonlyRecord<string, A>) => Separated<ReadonlyRecord<string, A>, ReadonlyRecord<string, A>> {
return (r) => {
const left: Record<string, A> = {}
const right: Record<string, A> = {}
for (const k in r) {
if (_.has.call(r, k)) {
const a = r[k]
if (predicateWithIndex(k, a)) {
right[k] = a
} else {
left[k] = a
}
}
}
return separated(left, right)
}
}
/**
* Maps a `ReadonlyRecord` with an iterating function that takes key and value and
* returns an `Option`, keeping only the `Some` values and discarding `None`s.
*
* @example
* import { filterMapWithIndex } from "fp-ts/ReadonlyRecord"
* import { option } from "fp-ts"
*
* const f = (key: string, a: number) => (a >= 0 ? option.some(`${key}${a}`) : option.none);
* assert.deepStrictEqual(filterMapWithIndex(f)({ a: -1, b: 2, c: 3 }), {
* b: "b2",
* c: "c3",
* });
*
* @category combinators
* @since 2.5.0
*/
export function filterMapWithIndex<K extends string, A, B>(
f: (key: K, a: A) => Option<B>
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<string, B>
export function filterMapWithIndex<A, B>(
f: (key: string, a: A) => Option<B>
): (fa: ReadonlyRecord<string, A>) => ReadonlyRecord<string, B> {
return (r) => {
const out: Record<string, B> = {}
for (const k in r) {
if (_.has.call(r, k)) {
const ob = f(k, r[k])
if (_.isSome(ob)) {
out[k] = ob.value
}
}
}
return out
}
}
/**
* Produce a new `ReadonlyRecord` keeping only the entries that satisfy
* a predicate taking key and value as input.
*
* @example
* import { filterWithIndex } from "fp-ts/ReadonlyRecord"
*
* assert.deepStrictEqual(
* filterWithIndex((s: string, v: number) => s.length <= 1 && v > 0)({ a: 1, b: -2, ccc: 3 }),
* {
* a: 1,
* }
* );
*
* @since 2.5.0
*/
export function filterWithIndex<K extends string, A, B extends A>(
refinementWithIndex: RefinementWithIndex<K, A, B>
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<string, B>
export function filterWithIndex<K extends string, A>(
predicateWithIndex: PredicateWithIndex<K, A>
): <B extends A>(fb: ReadonlyRecord<K, B>) => ReadonlyRecord<string, B>
export function filterWithIndex<K extends string, A>(
predicateWithIndex: PredicateWithIndex<K, A>
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<string, A>
export function filterWithIndex<A>(
predicateWithIndex: PredicateWithIndex<string, A>
): (fa: ReadonlyRecord<string, A>) => ReadonlyRecord<string, A> {
return (fa) => {
const out: Record<string, A> = {}
let changed = false
for (const key in fa) {
if (_.has.call(fa, key)) {
const a = fa[key]
if (predicateWithIndex(key, a)) {
out[key] = a
} else {
changed = true
}
}
}
return changed ? out : fa
}
}
/**
* Create a `ReadonlyRecord` from a foldable collection of key/value pairs, using the
* specified `Magma` to combine values for duplicate keys.
*
* @since 2.5.0
*/
export function fromFoldable<F extends URIS3, A>(
M: Magma<A>,
F: Foldable3<F>
): <R, E>(fka: Kind3<F, R, E, readonly [string, A]>) => ReadonlyRecord<string, A>