playbook_ui 14.15.0.pre.alpha.play1910emptystatekitreactbeta6579 → 14.15.0.pre.alpha.play1917lodashremoval2of36615
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.
- checksums.yaml +4 -4
- data/app/pb_kits/playbook/_playbook.scss +2 -4
- data/app/pb_kits/playbook/pb_bar_graph/_bar_graph.tsx +1 -1
- data/app/pb_kits/playbook/pb_circle_chart/_circle_chart.tsx +1 -1
- data/app/pb_kits/playbook/pb_filter/Filter/SortMenu.tsx +2 -1
- data/app/pb_kits/playbook/pb_gauge/_gauge.tsx +1 -1
- data/app/pb_kits/playbook/pb_lightbox/Carousel/Slide.tsx +1 -1
- data/app/pb_kits/playbook/pb_lightbox/Carousel/Slides.tsx +1 -1
- data/app/pb_kits/playbook/pb_lightbox/Carousel/Thumbnails.tsx +1 -1
- data/app/pb_kits/playbook/pb_line_graph/_line_graph.tsx +1 -1
- data/app/pb_kits/playbook/pb_popover/_popover.tsx +1 -1
- data/app/pb_kits/playbook/pb_popover/popover.test.js +1 -1
- data/app/pb_kits/playbook/pb_text_input/inputMask.ts +2 -3
- data/app/pb_kits/playbook/pb_tooltip/docs/_tooltip_icon_rails.html.erb +22 -0
- data/app/pb_kits/playbook/pb_tooltip/docs/example.yml +1 -0
- data/app/pb_kits/playbook/pb_treemap_chart/_treemap_chart.tsx +1 -1
- data/app/pb_kits/playbook/pb_typeahead/_typeahead.tsx +16 -4
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_react_hook.jsx +66 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_react_hook.md +1 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async_custom_options.jsx +1 -1
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async_users.jsx +1 -1
- data/app/pb_kits/playbook/pb_typeahead/docs/example.yml +1 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/index.js +1 -0
- data/app/pb_kits/playbook/utilities/object.test.js +139 -1
- data/app/pb_kits/playbook/utilities/object.ts +74 -0
- data/app/pb_kits/playbook/utilities/text.ts +1 -1
- data/dist/chunks/_typeahead-CdVbIURO.js +36 -0
- data/dist/chunks/_weekday_stacked-BSTKfYPJ.js +45 -0
- data/dist/chunks/{lib-5OzNgeeu.js → lib-e7oBFFDo.js} +2 -2
- data/dist/chunks/{pb_form_validation-DGhKbZtO.js → pb_form_validation-CMXevI1t.js} +1 -1
- data/dist/chunks/vendor.js +1 -1
- data/dist/menu.yml +0 -8
- data/dist/playbook-doc.js +1 -1
- data/dist/playbook-rails-react-bindings.js +1 -1
- data/dist/playbook-rails.js +1 -1
- data/dist/playbook.css +1 -1
- data/lib/playbook/version.rb +1 -1
- metadata +9 -15
- data/app/pb_kits/playbook/pb_empty_state/_empty_state.scss +0 -38
- data/app/pb_kits/playbook/pb_empty_state/_empty_state.tsx +0 -199
- data/app/pb_kits/playbook/pb_empty_state/docs/_empty_state_alignment.jsx +0 -38
- data/app/pb_kits/playbook/pb_empty_state/docs/_empty_state_orientation.jsx +0 -19
- data/app/pb_kits/playbook/pb_empty_state/docs/_empty_state_size.jsx +0 -35
- data/app/pb_kits/playbook/pb_empty_state/docs/example.yml +0 -7
- data/app/pb_kits/playbook/pb_empty_state/docs/index.js +0 -3
- data/app/pb_kits/playbook/pb_empty_state/empty_state.test.jsx +0 -17
- data/app/pb_kits/playbook/pb_tooltip/docs/_tooltip_icon.erb +0 -10
- data/dist/chunks/_typeahead-D2Wtk1Vx.js +0 -36
- data/dist/chunks/_weekday_stacked-toeDpRDd.js +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7250a006190c9fd3ad50753379407ed9d56eaf7bbbe45bf675aa1cd3386adb19
|
4
|
+
data.tar.gz: f69959838cbc85aad8736d740beaed172aa92a0353751082eaf803c75b224940
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8de325ab14219ca126a14a1301d004bcdf80d7d1a6e92f608d540bd2bd4c85b46e0aee60db4f3ea8da19d6d55e123c0bbf870b6ef97426cafec47c6bb8a7e93d
|
7
|
+
data.tar.gz: a34995b8ba52bfd48be3268128b905d1e880a614994c75f48116eb07a271ce23a5627c340fd8061640def912fe0ab1352539003aef732939c9267674ea5d2b75
|
@@ -1,5 +1,4 @@
|
|
1
1
|
|
2
|
-
|
3
2
|
@import 'pb_advanced_table/advanced_table';
|
4
3
|
@import 'pb_avatar/avatar';
|
5
4
|
@import 'pb_background/background';
|
@@ -15,8 +14,8 @@
|
|
15
14
|
@import 'pb_circle_chart/circle_chart';
|
16
15
|
@import 'pb_circle_icon_button/circle_icon_button';
|
17
16
|
@import 'pb_collapsible/collapsible';
|
18
|
-
@import 'pb_contact/contact';
|
19
17
|
@import 'pb_copy_button/copy_button';
|
18
|
+
@import 'pb_contact/contact';
|
20
19
|
@import 'pb_currency/currency';
|
21
20
|
@import 'pb_dashboard_value/dashboard_value';
|
22
21
|
@import 'pb_date/date';
|
@@ -46,7 +45,6 @@
|
|
46
45
|
@import 'pb_highlight/highlight';
|
47
46
|
@import 'pb_home_address_street/home_address_street';
|
48
47
|
@import 'pb_icon/icon';
|
49
|
-
@import 'pb_icon_button/icon_button';
|
50
48
|
@import 'pb_icon_circle/icon_circle';
|
51
49
|
@import 'pb_icon_stat_value/icon_stat_value';
|
52
50
|
@import 'pb_icon_value/icon_value';
|
@@ -111,7 +109,7 @@
|
|
111
109
|
@import 'pb_user_badge/user_badge';
|
112
110
|
@import 'pb_walkthrough/walkthrough';
|
113
111
|
@import 'pb_weekday_stacked/weekday_stacked';
|
114
|
-
@import '
|
112
|
+
@import 'pb_icon_button/icon_button';
|
115
113
|
@import 'utilities/mixins';
|
116
114
|
@import 'utilities/spacing';
|
117
115
|
@import 'utilities/cursor';
|
@@ -7,7 +7,7 @@ import Highcharts from "highcharts";
|
|
7
7
|
import { highchartsTheme } from "../pb_dashboard/pbChartsLightTheme";
|
8
8
|
import { highchartsDarkTheme } from "../pb_dashboard/pbChartsDarkTheme";
|
9
9
|
import mapColors from "../pb_dashboard/pbChartsColorsHelper";
|
10
|
-
import { merge } from '
|
10
|
+
import { merge } from '../utilities/object'
|
11
11
|
|
12
12
|
import classnames from "classnames";
|
13
13
|
|
@@ -10,7 +10,7 @@ import { highchartsDarkTheme } from "../pb_dashboard/pbChartsDarkTheme";
|
|
10
10
|
import mapColors from "../pb_dashboard/pbChartsColorsHelper";
|
11
11
|
import { globalProps } from "../utilities/globalProps";
|
12
12
|
import { buildAriaProps, buildDataProps, buildHtmlProps } from "../utilities/props";
|
13
|
-
import { merge } from '
|
13
|
+
import { merge } from '../utilities/object'
|
14
14
|
|
15
15
|
type CircleChartProps = {
|
16
16
|
align?: "left" | "right" | "center";
|
@@ -13,7 +13,7 @@ import typography from "../tokens/exports/_typography.module.scss";
|
|
13
13
|
import { buildAriaProps, buildCss, buildDataProps, buildHtmlProps } from "../utilities/props";
|
14
14
|
import { globalProps } from "../utilities/globalProps";
|
15
15
|
import { GenericObject } from "../types";
|
16
|
-
import { merge } from '
|
16
|
+
import { merge } from '../utilities/object'
|
17
17
|
|
18
18
|
type GaugeProps = {
|
19
19
|
aria: { [key: string]: string };
|
@@ -8,7 +8,7 @@ import Highcharts from "highcharts";
|
|
8
8
|
import { highchartsTheme } from "../pb_dashboard/pbChartsLightTheme";
|
9
9
|
import { highchartsDarkTheme } from "../pb_dashboard/pbChartsDarkTheme";
|
10
10
|
import mapColors from "../pb_dashboard/pbChartsColorsHelper";
|
11
|
-
import { merge } from '
|
11
|
+
import { merge } from '../utilities/object'
|
12
12
|
|
13
13
|
type LineGraphProps = {
|
14
14
|
align?: "left" | "right" | "center";
|
@@ -84,7 +84,7 @@ const Popover = (props: PbPopoverProps) => {
|
|
84
84
|
} = props;
|
85
85
|
|
86
86
|
const items = globalProps(props).split(' ')
|
87
|
-
const filteredItems = items.filter(item => !item.includes('
|
87
|
+
const filteredItems = items.filter(item => !item.includes('min-width') && !item.includes('width'))
|
88
88
|
const filteredGlobalProps = filteredItems.join(' ')
|
89
89
|
const popoverSpacing =
|
90
90
|
filteredGlobalProps.includes("dark") || !filteredGlobalProps
|
@@ -181,7 +181,7 @@ const PopoverTestClicktoClose3 = () => {
|
|
181
181
|
const btn = screen.getByText(/click me/i)
|
182
182
|
fireEvent.click(btn);
|
183
183
|
const kit = screen.getByText("Click Anywhere");
|
184
|
-
expect(kit).toHaveClass("pb_popover_body
|
184
|
+
expect(kit).toHaveClass("pb_popover_body p_sm overflow_handling");
|
185
185
|
});
|
186
186
|
|
187
187
|
test("closes Popover on click anywhere", async () => {
|
@@ -17,7 +17,7 @@ const formatCurrencyDefaultValue = (value: string): string => {
|
|
17
17
|
|
18
18
|
// Parse the numeric value as a float to handle decimals
|
19
19
|
const dollars = parseFloat(numericValue)
|
20
|
-
if (isNaN(dollars)
|
20
|
+
if (isNaN(dollars)) return ''
|
21
21
|
|
22
22
|
// Format as currency
|
23
23
|
return new Intl.NumberFormat('en-US', {
|
@@ -30,10 +30,9 @@ const formatCurrencyDefaultValue = (value: string): string => {
|
|
30
30
|
const formatCurrency = (value: string): string => {
|
31
31
|
const numericValue = value.replace(/[^0-9]/g, '').slice(0, 15)
|
32
32
|
|
33
|
-
if (!numericValue) return ''
|
33
|
+
if (!numericValue || numericValue === "00") return ''
|
34
34
|
|
35
35
|
const dollars = parseFloat((parseInt(numericValue) / 100).toFixed(2))
|
36
|
-
if (dollars === 0) return ''
|
37
36
|
|
38
37
|
return new Intl.NumberFormat('en-US', {
|
39
38
|
style: 'currency',
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<%= pb_rails "flex", props: { flex_direction: "row", gap: "md", wrap: true } do %>
|
2
|
+
<%= pb_rails "button", props: { id: "tool-tip-with-icon-1", text: "Tooltip With Icon" } %>
|
3
|
+
<%= pb_rails "tooltip", props: { trigger_element_selector: "#tool-tip-with-icon-1", tooltip_id: "tool-tip-with-icon-1", position: "top" } do %>
|
4
|
+
<%= pb_rails("icon", props: { icon: "paper-plane", fixed_width: true, color: "white", size: "1x", padding_right: "xxs" }) %>
|
5
|
+
Send Email
|
6
|
+
<% end %>
|
7
|
+
<%= pb_rails "button", props: { id: "tool-tip-with-icon-2", text: "Tooltip With Icon" } %>
|
8
|
+
<%= pb_rails "tooltip", props: { trigger_element_selector: "#tool-tip-with-icon-2", tooltip_id: "tool-tip-with-icon-2", position: "bottom" } do %>
|
9
|
+
<%= pb_rails("icon", props: { icon: "paper-plane", fixed_width: true, color: "white", size: "1x", padding_right: "xxs" }) %>
|
10
|
+
Send Email
|
11
|
+
<% end %>
|
12
|
+
<%= pb_rails "button", props: { id: "tool-tip-with-icon-3", text: "Tooltip With Icon" } %>
|
13
|
+
<%= pb_rails "tooltip", props: { trigger_element_selector: "#tool-tip-with-icon-3", tooltip_id: "tool-tip-with-icon-3", position: "left" } do %>
|
14
|
+
<%= pb_rails("icon", props: { icon: "paper-plane", fixed_width: true, color: "white", size: "1x", padding_right: "xxs" }) %>
|
15
|
+
Send Email
|
16
|
+
<% end %>
|
17
|
+
<%= pb_rails "button", props: { id: "tool-tip-with-icon-4", text: "Tooltip With Icon" } %>
|
18
|
+
<%= pb_rails "tooltip", props: { trigger_element_selector: "#tool-tip-with-icon-4", tooltip_id: "tool-tip-with-icon-4", position: "right" } do %>
|
19
|
+
<%= pb_rails("icon", props: { icon: "paper-plane", fixed_width: true, color: "white", size: "1x", padding_right: "xxs" }) %>
|
20
|
+
Send Email
|
21
|
+
<% end %>
|
22
|
+
<% end %>
|
@@ -4,6 +4,7 @@ examples:
|
|
4
4
|
- tooltip_default: Default
|
5
5
|
- tooltip_interaction: Content Interaction
|
6
6
|
- tooltip_selectors: Using Common Selectors
|
7
|
+
- tooltip_icon_rails: Tooltip with Icon
|
7
8
|
- tooltip_with_icon_circle: Icon Circle Tooltip
|
8
9
|
- tooltip_delay_rails: Delay
|
9
10
|
- tooltip_show_tooltip: Show Tooltip
|
@@ -10,7 +10,7 @@ import { highchartsTheme } from "../pb_dashboard/pbChartsLightTheme";
|
|
10
10
|
import { highchartsDarkTheme } from "../pb_dashboard/pbChartsDarkTheme";
|
11
11
|
import mapColors from "../pb_dashboard/pbChartsColorsHelper";
|
12
12
|
import treemap from 'highcharts/modules/treemap'
|
13
|
-
import { merge } from '
|
13
|
+
import { merge } from '../utilities/object'
|
14
14
|
|
15
15
|
type TreemapChartProps = {
|
16
16
|
chartData: {
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import React, { useState, useEffect} from 'react'
|
1
|
+
import React, { useState, useEffect, forwardRef} from 'react'
|
2
2
|
import Select from 'react-select'
|
3
3
|
import AsyncSelect from 'react-select/async'
|
4
4
|
import CreateableSelect from 'react-select/creatable'
|
@@ -49,6 +49,7 @@ type TypeaheadProps = {
|
|
49
49
|
options?: Array<{ label: string; value?: string }>,
|
50
50
|
marginBottom?: "none" | "xxs" | "xs" | "sm" | "md" | "lg" | "xl",
|
51
51
|
pillColor?: "primary" | "neutral" | "success" | "warning" | "error" | "info" | "data_1" | "data_2" | "data_3" | "data_4" | "data_5" | "data_6" | "data_7" | "data_8" | "windows" | "siding" | "roofing" | "doors" | "gutters" | "solar" | "insulation" | "accessories",
|
52
|
+
onChange?: any,
|
52
53
|
optionsByContext?: Record<string, Array<{ label: string; value?: string }>>
|
53
54
|
searchContextSelector?: string,
|
54
55
|
clearOnContextChange?: boolean,
|
@@ -71,8 +72,7 @@ type TagOnChangeValues = {
|
|
71
72
|
* @constant {React.ReactComponent} Typeahead
|
72
73
|
* @param {TypeaheadProps} props - props as described at https://react-select.com/props
|
73
74
|
*/
|
74
|
-
|
75
|
-
const Typeahead = ({
|
75
|
+
const Typeahead = forwardRef<HTMLInputElement, TypeaheadProps>(({
|
76
76
|
async,
|
77
77
|
className,
|
78
78
|
components = {},
|
@@ -83,9 +83,11 @@ const Typeahead = ({
|
|
83
83
|
getOptionValue,
|
84
84
|
htmlOptions = {},
|
85
85
|
id,
|
86
|
+
name,
|
86
87
|
loadOptions = noop,
|
87
88
|
marginBottom = "sm",
|
88
89
|
pillColor,
|
90
|
+
onChange,
|
89
91
|
optionsByContext = {},
|
90
92
|
searchContextSelector,
|
91
93
|
clearOnContextChange = false,
|
@@ -156,6 +158,15 @@ const Typeahead = ({
|
|
156
158
|
)
|
157
159
|
|
158
160
|
const handleOnChange = (_data: SelectValueType, { action, option, removedValue }: TagOnChangeValues) => {
|
161
|
+
if (onChange) {
|
162
|
+
const isReactHookForm = onChange.toString().includes("target")
|
163
|
+
if (isReactHookForm) {
|
164
|
+
onChange({ target: { name, value: _data } })
|
165
|
+
} else {
|
166
|
+
onChange(_data)
|
167
|
+
}
|
168
|
+
}
|
169
|
+
|
159
170
|
if (action === 'select-option') {
|
160
171
|
if (selectProps.onMultiValueClick) selectProps.onMultiValueClick(option)
|
161
172
|
const multiValueClearEvent = new CustomEvent(`pb-typeahead-kit-${selectProps.id}-result-option-select`, { detail: option ? option : _data })
|
@@ -199,10 +210,11 @@ const Typeahead = ({
|
|
199
210
|
/>
|
200
211
|
</div>
|
201
212
|
)
|
202
|
-
}
|
213
|
+
})
|
203
214
|
|
204
215
|
Object.keys(kitComponents).forEach((k) => {
|
205
216
|
(Typeahead as GenericObject)[k] = (kitComponents as {[key: string]: unknown})[k]
|
206
217
|
})
|
207
218
|
|
219
|
+
Typeahead.displayName = 'Typeahead'
|
208
220
|
export default Typeahead
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import React from 'react'
|
2
|
+
|
3
|
+
import { Typeahead, Title } from 'playbook-ui'
|
4
|
+
import { useForm } from 'react-hook-form'
|
5
|
+
|
6
|
+
const languages = [
|
7
|
+
{ label: 'JavaScript', value: '1995', category: 'Web Development' },
|
8
|
+
{ label: 'Python', value: '1991', category: 'General Purpose' },
|
9
|
+
{ label: 'Java', value: '1995', category: 'Enterprise' },
|
10
|
+
{ label: 'C++', value: '1985', category: 'Systems Programming' },
|
11
|
+
{ label: 'Go', value: '2009', category: 'Systems Programming' },
|
12
|
+
{ label: 'Rust', value: '2010', category: 'Systems Programming' },
|
13
|
+
{ label: 'Swift', value: '2014', category: 'Mobile Development' },
|
14
|
+
{ label: 'Kotlin', value: '2011', category: 'Mobile Development' },
|
15
|
+
{ label: 'Ruby', value: '1995', category: 'General Purpose' },
|
16
|
+
{ label: 'PHP', value: '1995', category: 'Web Development' },
|
17
|
+
]
|
18
|
+
|
19
|
+
const colors = [
|
20
|
+
{ label: 'Orange', value: '#FFA500' },
|
21
|
+
{ label: 'Red', value: '#FF0000' },
|
22
|
+
{ label: 'Green', value: '#00FF00' },
|
23
|
+
{ label: 'Blue', value: '#0000FF' },
|
24
|
+
]
|
25
|
+
|
26
|
+
const TypeaheadReactHook = (props) => {
|
27
|
+
const { register, watch } = useForm()
|
28
|
+
|
29
|
+
const selectedLanguages = watch('languages')
|
30
|
+
const selectedColor = watch('color')
|
31
|
+
|
32
|
+
return (
|
33
|
+
<>
|
34
|
+
<Typeahead
|
35
|
+
isMulti
|
36
|
+
label="Multi Select Languages"
|
37
|
+
multiKit="language"
|
38
|
+
options={languages}
|
39
|
+
{...props}
|
40
|
+
{...register('languages')}
|
41
|
+
/>
|
42
|
+
<Title
|
43
|
+
size={4}
|
44
|
+
text='Selected Languages'
|
45
|
+
/>
|
46
|
+
{selectedLanguages && selectedLanguages.map(language => (
|
47
|
+
<p key={language.label}>{`${language.label} - ${language.value} - ${language.category}`}</p>
|
48
|
+
))}
|
49
|
+
|
50
|
+
<Typeahead
|
51
|
+
label="Colors"
|
52
|
+
marginTop="lg"
|
53
|
+
options={colors}
|
54
|
+
{...props}
|
55
|
+
{...register('color')}
|
56
|
+
/>
|
57
|
+
<Title
|
58
|
+
size={4}
|
59
|
+
text='Selected Color'
|
60
|
+
/>
|
61
|
+
<p>{ selectedColor && `${selectedColor.label} - ${selectedColor.value}`}</p>
|
62
|
+
</>
|
63
|
+
)
|
64
|
+
}
|
65
|
+
|
66
|
+
export default TypeaheadReactHook
|
@@ -0,0 +1 @@
|
|
1
|
+
You can pass `react-hook-form` props to the Typeahead kit.
|
@@ -49,7 +49,7 @@ const TypeaheadWithPillsAsyncCustomOptions = (props) => {
|
|
49
49
|
* @summary - for doc example purposes only
|
50
50
|
*/
|
51
51
|
const handleOnMultiValueClick = (value) => {
|
52
|
-
alert(`You
|
52
|
+
alert(`You added the user: "${value.label}"`)
|
53
53
|
}
|
54
54
|
|
55
55
|
return (
|
@@ -56,7 +56,7 @@ const TypeaheadWithPillsAsyncUsers = (props) => {
|
|
56
56
|
* @summary - for doc example purposes only
|
57
57
|
*/
|
58
58
|
const handleOnMultiValueClick = (value) => {
|
59
|
-
alert(`You
|
59
|
+
alert(`You added the user: "${value.label}"`)
|
60
60
|
}
|
61
61
|
|
62
62
|
return (
|
@@ -13,3 +13,4 @@ export { default as TypeaheadCustomMenuList } from './_typeahead_custom_menu_lis
|
|
13
13
|
export { default as TypeaheadMarginBottom } from './_typeahead_margin_bottom.jsx'
|
14
14
|
export { default as TypeaheadWithPillsColor } from './_typeahead_with_pills_color.jsx'
|
15
15
|
export { default as TypeaheadTruncatedText } from './_typeahead_truncated_text.jsx'
|
16
|
+
export { default as TypeaheadReactHook } from './_typeahead_react_hook.jsx'
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { isEmpty, get, isString, uniqueId, omitBy } from './object';
|
1
|
+
import { isEmpty, get, isString, uniqueId, omitBy, noop, merge, filter, find, partial } from './object';
|
2
2
|
|
3
3
|
describe('Lodash functions', () => {
|
4
4
|
describe('isEmpty', () => {
|
@@ -96,4 +96,142 @@ describe('Lodash functions', () => {
|
|
96
96
|
expect(objWithSmallValues).toEqual(obj);
|
97
97
|
});
|
98
98
|
});
|
99
|
+
|
100
|
+
describe('noop', () => {
|
101
|
+
test('should do nothing and return undefined', () => {
|
102
|
+
expect(noop()).toBeUndefined();
|
103
|
+
});
|
104
|
+
});
|
105
|
+
|
106
|
+
describe('merge', () => {
|
107
|
+
test('merges two objects correctly', () => {
|
108
|
+
const obj1 = { a: 1, b: { x: 10 } };
|
109
|
+
const obj2 = { b: { y: 20 }, c: 3 };
|
110
|
+
expect(merge(obj1, obj2)).toEqual({ a: 1, b: { x: 10, y: 20 }, c: 3 });
|
111
|
+
});
|
112
|
+
|
113
|
+
test('when keys repeat use last occurrence value', () => {
|
114
|
+
const obj1 = { a: 1 };
|
115
|
+
const obj2 = { b: 2 };
|
116
|
+
const obj3 = { a: 3, c: 4 };
|
117
|
+
expect(merge(obj1, obj2, obj3)).toEqual({ a: 3, b: 2, c: 4 });
|
118
|
+
});
|
119
|
+
|
120
|
+
test('ignores non-object arguments', () => {
|
121
|
+
expect(merge(null, { a: 1 })).toEqual({ a: 1 });
|
122
|
+
expect(merge(undefined, { a: 1 })).toEqual({ a: 1 });
|
123
|
+
});
|
124
|
+
});
|
125
|
+
|
126
|
+
describe('filter', () => {
|
127
|
+
test('filters an array using a function predicate', () => {
|
128
|
+
const arr = [1, 2, 3, 4, 5];
|
129
|
+
const isEven = (n) => n % 2 === 0;
|
130
|
+
expect(filter(arr, isEven)).toEqual([2, 4]);
|
131
|
+
});
|
132
|
+
|
133
|
+
test('filters an array using a string predicate', () => {
|
134
|
+
const arr = [
|
135
|
+
{ active: true, name: 'John' },
|
136
|
+
{ active: false, name: 'Jane' },
|
137
|
+
{ active: true, name: 'Doe' }
|
138
|
+
];
|
139
|
+
expect(filter(arr, 'active')).toEqual([
|
140
|
+
{ active: true, name: 'John' },
|
141
|
+
{ active: true, name: 'Doe' }
|
142
|
+
]);
|
143
|
+
});
|
144
|
+
|
145
|
+
test('filters an array using an array predicate', () => {
|
146
|
+
const arr = [
|
147
|
+
{ type: 'fruit', name: 'apple' },
|
148
|
+
{ type: 'vegetable', name: 'carrot' },
|
149
|
+
{ type: 'fruit', name: 'banana' }
|
150
|
+
];
|
151
|
+
expect(filter(arr, ['type', 'fruit'])).toEqual([
|
152
|
+
{ type: 'fruit', name: 'apple' },
|
153
|
+
{ type: 'fruit', name: 'banana' }
|
154
|
+
]);
|
155
|
+
});
|
156
|
+
|
157
|
+
test('filters an array using an object predicate', () => {
|
158
|
+
const arr = [
|
159
|
+
{ type: 'fruit', name: 'apple', color: 'red' },
|
160
|
+
{ type: 'fruit', name: 'banana', color: 'yellow' },
|
161
|
+
{ type: 'vegetable', name: 'carrot', color: 'orange' }
|
162
|
+
];
|
163
|
+
expect(filter(arr, { type: 'fruit', color: 'red' })).toEqual([
|
164
|
+
{ type: 'fruit', name: 'apple', color: 'red' }
|
165
|
+
]);
|
166
|
+
});
|
167
|
+
});
|
168
|
+
|
169
|
+
describe('find', () => {
|
170
|
+
test('finds an element using a function predicate', () => {
|
171
|
+
const arr = [1, 2, 3, 4, 5];
|
172
|
+
const greaterThanThree = (n) => n > 3;
|
173
|
+
expect(find(arr, greaterThanThree)).toBe(4);
|
174
|
+
});
|
175
|
+
|
176
|
+
test('finds an element using a string predicate', () => {
|
177
|
+
const arr = [
|
178
|
+
{ active: false, name: 'John' },
|
179
|
+
{ active: true, name: 'Jane' },
|
180
|
+
{ active: true, name: 'Doe' }
|
181
|
+
];
|
182
|
+
expect(find(arr, 'active')).toEqual({ active: true, name: 'Jane' });
|
183
|
+
});
|
184
|
+
|
185
|
+
test('finds an element using an array predicate', () => {
|
186
|
+
const arr = [
|
187
|
+
{ type: 'fruit', name: 'apple' },
|
188
|
+
{ type: 'vegetable', name: 'carrot' },
|
189
|
+
{ type: 'fruit', name: 'banana' }
|
190
|
+
];
|
191
|
+
expect(find(arr, ['type', 'vegetable'])).toEqual({ type: 'vegetable', name: 'carrot' });
|
192
|
+
});
|
193
|
+
|
194
|
+
test('finds an element using an object predicate', () => {
|
195
|
+
const arr = [
|
196
|
+
{ type: 'fruit', name: 'apple', color: 'red' },
|
197
|
+
{ type: 'fruit', name: 'banana', color: 'yellow' },
|
198
|
+
{ type: 'vegetable', name: 'carrot', color: 'orange' }
|
199
|
+
];
|
200
|
+
expect(find(arr, { name: 'banana', color: 'yellow' })).toEqual({ type: 'fruit', name: 'banana', color: 'yellow' });
|
201
|
+
});
|
202
|
+
|
203
|
+
test('returns undefined if no element matches', () => {
|
204
|
+
const arr = [{ id: 1 }, { id: 2 }];
|
205
|
+
expect(find(arr, { id: 3 })).toBeUndefined();
|
206
|
+
});
|
207
|
+
});
|
208
|
+
|
209
|
+
describe('partial', () => {
|
210
|
+
function add(a, b, c) {
|
211
|
+
return a + b + c;
|
212
|
+
}
|
213
|
+
|
214
|
+
test('partials arguments without placeholders', () => {
|
215
|
+
const add5 = partial(add, 2, 3);
|
216
|
+
expect(add5(4)).toBe(9);
|
217
|
+
});
|
218
|
+
|
219
|
+
test('partials arguments with placeholders', () => {
|
220
|
+
const addWithPlaceholder = partial(add, partial.placeholder, 3, partial.placeholder);
|
221
|
+
expect(addWithPlaceholder(2, 4)).toBe(9);
|
222
|
+
});
|
223
|
+
|
224
|
+
test('returns correct result when all arguments are pre-filled', () => {
|
225
|
+
const addAll = partial(add, 1, 2, 3);
|
226
|
+
expect(addAll()).toBe(6);
|
227
|
+
});
|
228
|
+
|
229
|
+
test('appends extra arguments when provided', () => {
|
230
|
+
function join(...args) {
|
231
|
+
return args.join('_');
|
232
|
+
}
|
233
|
+
const joinPartial = partial(join, 'a');
|
234
|
+
expect(joinPartial('b', 'c')).toBe('a_b_c');
|
235
|
+
});
|
236
|
+
});
|
99
237
|
});
|
@@ -29,3 +29,77 @@ export const omitBy = (obj: Record<string, any>, predicate: (value: any, key: st
|
|
29
29
|
return result;
|
30
30
|
}, {})
|
31
31
|
}
|
32
|
+
|
33
|
+
export const noop = (): void => {
|
34
|
+
// empty
|
35
|
+
};
|
36
|
+
|
37
|
+
export const merge = (...objects: any[]): any => {
|
38
|
+
const isObject = (obj: any): boolean => obj && typeof obj === 'object';
|
39
|
+
const result = {} as any;
|
40
|
+
objects.forEach(obj => {
|
41
|
+
if (isObject(obj)) {
|
42
|
+
Object.keys(obj).forEach(key => {
|
43
|
+
const existingVal = result[key];
|
44
|
+
const newVal = obj[key];
|
45
|
+
if (isObject(existingVal) && isObject(newVal)) {
|
46
|
+
result[key] = merge(existingVal, newVal);
|
47
|
+
} else {
|
48
|
+
result[key] = newVal;
|
49
|
+
}
|
50
|
+
});
|
51
|
+
}
|
52
|
+
});
|
53
|
+
return result;
|
54
|
+
};
|
55
|
+
|
56
|
+
const createIteratee = (predicate: any) => {
|
57
|
+
if (typeof predicate === 'function') {
|
58
|
+
return predicate;
|
59
|
+
}
|
60
|
+
if (typeof predicate === 'string') {
|
61
|
+
return (obj: any) => obj[predicate];
|
62
|
+
}
|
63
|
+
if (Array.isArray(predicate)) {
|
64
|
+
const [key, value] = predicate;
|
65
|
+
return (obj: any) => obj[key] === value;
|
66
|
+
}
|
67
|
+
if (typeof predicate === 'object' && predicate !== null) {
|
68
|
+
return (obj: any) => {
|
69
|
+
for (const key in predicate) {
|
70
|
+
if (Object.prototype.hasOwnProperty.call(predicate, key)) {
|
71
|
+
if (obj[key] !== predicate[key]) return false;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
return true;
|
75
|
+
};
|
76
|
+
}
|
77
|
+
return () => false;
|
78
|
+
};
|
79
|
+
|
80
|
+
export const filter = <T>(array: T[], predicate: any): T[] => {
|
81
|
+
const iteratee = createIteratee(predicate);
|
82
|
+
return array.filter(iteratee);
|
83
|
+
};
|
84
|
+
|
85
|
+
export const find = <T>(array: T[], predicate: any): T | undefined => {
|
86
|
+
const iteratee = createIteratee(predicate);
|
87
|
+
return array.find(iteratee);
|
88
|
+
};
|
89
|
+
|
90
|
+
export const partial = <F extends (...args: unknown[]) => unknown>(
|
91
|
+
fn: F,
|
92
|
+
...partials: unknown[]
|
93
|
+
): ((...args: unknown[]) => ReturnType<F>) => {
|
94
|
+
const placeholder = partial.placeholder;
|
95
|
+
return (...args: unknown[]): ReturnType<F> => {
|
96
|
+
let argIndex = 0;
|
97
|
+
const finalArgs = partials.map(arg =>
|
98
|
+
arg === placeholder ? args[argIndex++] : arg
|
99
|
+
);
|
100
|
+
return fn(...(finalArgs.concat(args.slice(argIndex)) as Parameters<F>)) as ReturnType<F>;
|
101
|
+
};
|
102
|
+
};
|
103
|
+
|
104
|
+
partial.placeholder = Symbol();
|
105
|
+
export const _ = partial.placeholder;
|