-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove Duplicate Events
110 lines (85 loc) · 4.01 KB
/
Remove Duplicate Events
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
"Version 1: Es wird angenommen, dass das array passend sortiert ist."
groupBy := [:array :equals |
| lastElement result group |
result := {}.
[:element |
(equals value: element value: lastElement)
ifTrue: [group add: element]
ifFalse: [
group := Array arrayWithObject: element.
result add: group].
lastElement := element] value: @array.
result].
a := {1, 2, 2, 3, 4, 4, 4, 5, 6, 6}.
equals := [:a :b | a = b].
groupBy value: a value: equals.
"Version 2: Das array muss nicht sortiert sein, weil es sortiert wird."
groupBy := [:array :selectors |
| sortedArray lastElement result group |
sortedArray := array.
[:selector | sortedArray := sortedArray at: (selector value: sortedArray) sort] value: @ selectors reverse.
result := {}.
[:element |
((lastElement == nil) not and: [(selectors value: lastElement) = (selectors value: element) \ #&])
ifTrue: [group add: element]
ifFalse: [
group := Array arrayWithObject: element.
result add: group].
lastElement := element] value: @ sortedArray.
result].
testGroupBy := [
| array selectors expected result |
array := {4, 2, 1, 2, 5, 3, 4, 6, 6, 4}.
selectors := {#self}.
result := groupBy value: array value: selectors.
expected := {{1}, {2, 2}, {3}, {4, 4, 4}, {5}, {6, 6}}.
expected isEqual: result].
testGroupBy value.
eventsBlock := [:start :end |
| store calendars startDate endDate predicate |
(NSBundle bundleWithPath:'/System/Library/Frameworks/CalendarStore.framework') load.
store := CalCalendarStore defaultCalendarStore.
calendars := store calendars.
startDate := (start ++ ' 00:00:00 +0200') asDate.
endDate := (end ++ ' 00:00:00 +0200') asDate.
predicate := CalCalendarStore eventPredicateWithStartDate: startDate endDate: endDate calendars: calendars.
store eventsWithPredicate: predicate].
duplicateEventsBlock := [:start :end |
| events selectors groupedEvents |
events := eventsBlock value: start value: end.
selectors := {#startDate, #endDate, #title}.
groupedEvents := groupBy value: events value: selectors.
groupedEvents at: groupedEvents @ count > 1].
duplicateEvents := (duplicateEventsBlock value: '2006-01-01' value: '2011-01-01')
duplicateEvents \ #++ inspectWithSystem: sys blocks: {#startDate, #endDate, #title, #recurrenceRule, #self}
[:e | e startDate description ++ ' ' ++ e title] value: @@ duplicateEvents
[:e | e startDate description ++ ' ' ++ e title] value: @@ (duplicateEventsBlock value: '2007-01-01' value: '2008-01-01')
{#startDate, #endDate, #title} value: @@ (duplicateEventsBlock value: '2007-01-01' value: '2008-01-01')
eventsToDelete := [:events |
| oneWithRecurrenceRule |
oneWithRecurrenceRule := ([:each | (each recurrenceRule == nil) not] value: @ events) \ #|.
oneWithRecurrenceRule
ifTrue: [events at: ([:each | each recurrenceRule == nil] value: @ events)]
ifFalse: [{}]]
(eventsToDelete value: @ duplicateEvents) \ #++ inspectWithSystem: sys blocks: {#startDate, #endDate, #title, #recurrenceRule, #self}
deleteEvent := [:event | CalCalendarStore defaultCalendarStore removeEvent: event span: 0 error: nil].
deleteEvent value: @ ((eventsToDelete value: @ duplicateEvents) \ #++).
events := eventsBlock value: '2007-07-29' value: '2007-08-07'.
(eventsBlock value: '2009-02-24' value: '2009-02-28') inspectWithSystem: sys blocks: {#startDate, #endDate, #title, #recurrenceRule, #self}.
sortBy := [:array :selectors |
| sortedArray |
sortedArray := array.
[:selector | sortedArray := sortedArray at: (selector value:
sortedArray) sort] value: @ selectors reverse.
sortedArray].
sortBy value: {-1, 3, -3, 4, 2} value: {#self}
sortBy value: {-1, 3, -3, 4, 2} value: {#abs}
sortBy value: {-1, 3, -3, 4, 2} value: {#abs, #self}
Traps:
- Es gibt kein notNil! #=, #~= und auch #~~ liefern nil und können daher nicht verwendet werden!
- Wenn eine Bedingung nil ergibt, wird weder der ifTrue- noch der ifFalse-Zweig ausgeführt.
- Ein Assignment hat den Wert FSVoid.
- Man darf keine (mutable) Arrays in ein NSSet geben.
- yourself heißt self
Problem:
- Beim title müsste man trimWhiteSpace machen. Wie?