|
1 | 1 | // prettier-ignore |
2 | 2 | import { |
3 | | - array_all, array_any, array_chunk, array_combine, array_diff, array_diff_assoc, array_diff_key, array_diff_uassoc, array_filter, array_find_key, array_flip, array_intersect, array_intersect_assoc, array_intersect_key, array_intersect_uassoc, array_keys, array_map, array_merge, array_merge_recursive, array_pad, array_pop, array_push, array_replace, array_replace_recursive, array_reverse, array_search, array_shift, array_slice, array_splice, array_uintersect, array_unique, arsort, asort, count, empty, in_array, intval, isset, krsort, ksort, range, SORT_FLAG_CASE, SORT_LOCALE_STRING, SORT_NATURAL, SORT_NUMERIC, SORT_REGULAR, SORT_STRING, strcasecmp, strcmp, strcoll, strnatcasecmp, strnatcmp, uasort, uksort, unset, |
| 3 | + array_all, array_any, array_chunk, array_combine, array_diff, array_diff_assoc, array_diff_key, array_diff_uassoc, array_filter, array_find_key, array_flip, array_intersect, array_intersect_assoc, array_intersect_key, array_intersect_uassoc, array_keys, array_map, array_merge, array_merge_recursive, array_pad, array_pop, array_push, array_replace, array_replace_recursive, array_reverse, array_search, array_shift, array_slice, array_splice, array_uintersect, array_unique, arsort, asort, count, empty, in_array, intval, isset, krsort, ksort, range, SORT_FLAG_CASE, SORT_LOCALE_STRING, SORT_NATURAL, SORT_NUMERIC, SORT_REGULAR, SORT_STRING, strcasecmp, strcmp, strcoll, strnatcasecmp, strnatcmp, uasort, uksort, unset, |
4 | 4 | } from '@balboacodes/php-utils'; |
5 | 5 | import { Arr } from './Arr'; |
| 6 | +import { Conditionable } from './Concerns/Conditionable'; |
| 7 | +import { use } from './Concerns/decorator'; |
6 | 8 | import { data_get, data_has, value } from './helpers'; |
7 | 9 |
|
8 | 10 | type Enumerable<TKey extends number | string, TValue> = TValue[] | Record<TKey, TValue> | Collection<TKey, TValue>; |
9 | 11 |
|
| 12 | +export interface Collection<TKey extends number | string, TValue> extends Conditionable {} |
| 13 | + |
| 14 | +@use(Conditionable) |
10 | 15 | export class Collection<TKey extends number | string, TValue> { |
11 | 16 | /** |
12 | 17 | * The items contained in the collection. |
@@ -1638,25 +1643,6 @@ export class Collection<TKey extends number | string, TValue> { |
1638 | 1643 | return this.unique(key, true); |
1639 | 1644 | } |
1640 | 1645 |
|
1641 | | - /** |
1642 | | - * Apply the callback if the given "value" is (or resolves to) falsy. |
1643 | | - */ |
1644 | | - public unless<TUnlessParameter, TUnlessReturnType>( |
1645 | | - value: ((instance: this) => TUnlessParameter) | TUnlessParameter, |
1646 | | - callback: (instance: this, value: TUnlessParameter) => TUnlessReturnType, |
1647 | | - defaultValue?: (instance: this, value: TUnlessParameter) => TUnlessReturnType, |
1648 | | - ): this | TUnlessReturnType { |
1649 | | - value = typeof value === 'function' ? (value as Function)(this) : value; |
1650 | | - |
1651 | | - if (!value) { |
1652 | | - return callback(this, value as TUnlessParameter) ?? this; |
1653 | | - } else if (defaultValue) { |
1654 | | - return defaultValue(this, value as TUnlessParameter) ?? this; |
1655 | | - } |
1656 | | - |
1657 | | - return this; |
1658 | | - } |
1659 | | - |
1660 | 1646 | /** |
1661 | 1647 | * Apply the callback unless the collection is empty. |
1662 | 1648 | */ |
@@ -1709,29 +1695,6 @@ export class Collection<TKey extends number | string, TValue> { |
1709 | 1695 | return new Collection({ ...Object.values(this.items) }) as any; |
1710 | 1696 | } |
1711 | 1697 |
|
1712 | | - /** |
1713 | | - * Apply the callback if the given "value" is (or resolves to) truthy. |
1714 | | - */ |
1715 | | - public when<TWhenParameter, TWhenReturnType>( |
1716 | | - when?: ((instance: this) => TWhenParameter) | TWhenParameter, |
1717 | | - callback?: (instance: this, when: TWhenParameter) => TWhenReturnType, |
1718 | | - defaultValue?: (instance: this, when: TWhenParameter) => TWhenReturnType, |
1719 | | - ): this | TWhenReturnType { |
1720 | | - when = typeof when === 'function' ? (when as Function)(this) : when; |
1721 | | - |
1722 | | - if (arguments.length === 0) { |
1723 | | - return this; |
1724 | | - } |
1725 | | - |
1726 | | - if (when) { |
1727 | | - return callback?.(this, when as any) ?? this; |
1728 | | - } else if (defaultValue) { |
1729 | | - return defaultValue(this, when as any) ?? this; |
1730 | | - } |
1731 | | - |
1732 | | - return this; |
1733 | | - } |
1734 | | - |
1735 | 1698 | /** |
1736 | 1699 | * Apply the callback if the collection is empty. |
1737 | 1700 | */ |
|
0 commit comments