@01-edu/shared 1.2.6 → 1.2.8

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.
Files changed (3) hide show
  1. package/attrs-defs.js +2 -113
  2. package/package.json +1 -1
  3. package/toolbox.js +27 -9
package/attrs-defs.js CHANGED
@@ -2622,7 +2622,7 @@ types.rankDefinitionName = Literal('', {
2622
2622
  })
2623
2623
 
2624
2624
  types.rankDefinition = TypeObject({
2625
- label: 'Rank definition',
2625
+ label: 'Rank requirements',
2626
2626
  type: {
2627
2627
  name: types.rankDefinitionName,
2628
2628
  level: Literal(MAX_LEVEL, {
@@ -2630,7 +2630,6 @@ types.rankDefinition = TypeObject({
2630
2630
  editable: true,
2631
2631
  required: true,
2632
2632
  // to put max level first in options - could also be [MAX_LEVEL, ...arrayOf(MAX_LEVEL -1, 0)]
2633
- options: arrayOf(MAX_LEVEL, 0).reverse(),
2634
2633
  check: (level, object) => {
2635
2634
  checkIntegerInBetween(level, 0, MAX_LEVEL)
2636
2635
 
@@ -2651,7 +2650,7 @@ types.rankDefinition = TypeObject({
2651
2650
  },
2652
2651
  })
2653
2652
  const sharedRanksDefinitions = {
2654
- label: 'Ranks definitions',
2653
+ label: 'Rank requirements',
2655
2654
  instruction: 'List of ranks access conditions',
2656
2655
  editable: true,
2657
2656
  restrictive: true,
@@ -2668,121 +2667,11 @@ const sharedRanksDefinitions = {
2668
2667
  },
2669
2668
  }
2670
2669
 
2671
- types.relatesToContent = Literal('', {
2672
- label: 'Content',
2673
- instruction:
2674
- 'Select a child object to link this learning path status to. Each child can only be linked once.',
2675
- editable: true,
2676
- restrictive: true,
2677
- options: (object, value) => {
2678
- const learnPathStatus = object.attrs?.learnPathStatus
2679
- ?.flatMap(({ relatesTo }) => relatesTo)
2680
- .filter(v => v !== value)
2681
-
2682
- const options = object.children ? Object.keys(object.children) : []
2683
- return !learnPathStatus
2684
- ? options
2685
- : options.filter(k => !learnPathStatus.includes(k))
2686
- },
2687
- })
2688
-
2689
- types.relatesToArray = TypeObject({
2690
- label: 'Relates to',
2691
- instruction:
2692
- 'Link one or more child objects to this learning path status. You can add multiple links by clicking the add button. If student did at least one he will have that status',
2693
- editable: true,
2694
- required: true,
2695
- restrictive: true,
2696
- value: (...args) => [
2697
- mapValues(types.relatesToArray.type, subDef =>
2698
- getDefaultValue(subDef, ...args),
2699
- ),
2700
- ],
2701
- type: [types.relatesToContent],
2702
- })
2703
-
2704
- types.learnerPathStatusName = Literal('', {
2705
- label: 'Name',
2706
- editable: true,
2707
- required: true,
2708
- primary: true,
2709
- })
2710
- types.definitionRelatesTo = TypeObject({
2711
- label: 'Status Definition',
2712
- type: {
2713
- name: types.learnerPathStatusName,
2714
- milestone: Literal('', {
2715
- label: 'Hover description',
2716
- instruction:
2717
- 'Text that appears when users hover over this item. Use this to provide additional context or helpful information.',
2718
- editable: true,
2719
- }),
2720
- relatesTo: types.relatesToArray,
2721
- },
2722
- check: (value, object) => {
2723
- const { relatesTo: currentRelatesTo, name } = value
2724
- const { learnPathStatus } = object.attrs
2725
- const previousValue = learnPathStatus.find(
2726
- ({ relatesTo }) =>
2727
- !(
2728
- currentRelatesTo.length !== relatesTo.length ||
2729
- currentRelatesTo.some((val, i) => val !== relatesTo[i])
2730
- ),
2731
- )
2732
- if (!previousValue) return
2733
-
2734
- const isDifferent =
2735
- previousValue.relatesTo.length !== currentRelatesTo.length ||
2736
- previousValue.relatesTo.some((val, i) => val !== currentRelatesTo[i])
2737
-
2738
- const hasSameKey = previousValue.name === name
2739
- if (!isDifferent && hasSameKey) return
2740
-
2741
- const definitionsWithSameName = object.attrs.learnPathStatus?.find(
2742
- v => v.name === name,
2743
- )
2744
- if (definitionsWithSameName) {
2745
- throw Error(
2746
- `Name "${name}" is already set for a learner path status! A given name can only be attributed once.`,
2747
- )
2748
- }
2749
- },
2750
- })
2751
-
2752
- const learnPathStatus = {
2753
- label: 'Learner Path Status',
2754
- instruction: 'List of Learner Status During Campus Steps',
2755
- editable: true,
2756
- restrictive: true,
2757
- value: (...args) => [
2758
- mapValues(types.learnPathStatus.type, subDef =>
2759
- getDefaultValue(subDef, ...args),
2760
- ),
2761
- ],
2762
- type: [types.definitionRelatesTo],
2763
- check: (definition, object) => {
2764
- if (!object.childrenRelation.length) {
2765
- throw Error('Must contain at least one relation')
2766
- }
2767
- if (!definition?.length || !Array.isArray(definition)) {
2768
- throw Error('Must be a non empty array')
2769
- }
2770
- },
2771
- }
2772
-
2773
2670
  attrs.ranksDefinitions = {
2774
2671
  module: sharedRanksDefinitions,
2775
2672
  piscine: sharedRanksDefinitions,
2776
2673
  }
2777
2674
 
2778
- attrs.learnPathStatus = {
2779
- // the learn path status will define what the student is during the steps
2780
- // - applicant (user that registered to the platform and passed the onboarding steps, up until being accepted to participate on the selection piscine/program)
2781
- // - candidate (user that is participating on the selection piscine)
2782
- // - learner (user that succeeded on the selection piscine and is participating on the module event)
2783
- campus: learnPathStatus,
2784
- }
2785
-
2786
2675
  const sharedRegistrationDuration = {
2787
2676
  label: 'Registration duration (to an event)',
2788
2677
  required: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@01-edu/shared",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "scripts": {
package/toolbox.js CHANGED
@@ -166,19 +166,25 @@ export const formatedDurationColons = (seconds, { hoursOnly } = {}) => {
166
166
 
167
167
  export const formatedMS = ms => formatedDuration(Math.round(ms / SEC))
168
168
  export const timeUnits = [
169
- { label: 'y', size: 31536000 },
170
- { label: 'w', size: 604800 },
171
- { label: 'd', size: 86400 },
172
- { label: 'h', size: 3600 },
173
- { label: 'm', size: 60 },
174
- { label: 's', size: 1 },
169
+ { label: 'y', fullLabel: 'year', size: 31536000 },
170
+ { label: 'w', fullLabel: 'week', size: 604800 },
171
+ { label: 'd', fullLabel: 'day', size: 86400 },
172
+ { label: 'h', fullLabel: 'hour', size: 3600 },
173
+ { label: 'm', fullLabel: 'minute', size: 60 },
174
+ { label: 's', fullLabel: 'second', size: 1 },
175
175
  ]
176
- export const formatedDuration = (seconds, { noSeconds } = {}) => {
176
+ export const formatedDuration = (
177
+ seconds,
178
+ { noSeconds, stopFirst, useFullLabel } = {},
179
+ ) => {
177
180
  const parts = []
178
- for (const { label, size } of timeUnits) {
181
+ for (const { label, size, fullLabel } of timeUnits) {
179
182
  const value = Math.floor(seconds / size)
180
183
  if (value !== 0) {
181
- parts.push(`${value}${label}`)
184
+ parts.push(
185
+ `${value}${useFullLabel ? ` ${fullLabel}` : label}${value > 1 && useFullLabel ? 's' : ''}`,
186
+ )
187
+ if (stopFirst) return parts
182
188
  seconds -= value * size
183
189
  }
184
190
  }
@@ -188,6 +194,18 @@ export const formatedDuration = (seconds, { noSeconds } = {}) => {
188
194
  return parts.join(' ') || (noSeconds ? '0m' : '0s')
189
195
  }
190
196
 
197
+ // Returns the difference between two dates in the following format: "3 months"
198
+ // Always returns the largest unit only
199
+ // If only one date is provided, it returns the difference between that date and now, the value is absolute
200
+ export const timeDiff = (date1, date2 = Date.now()) => {
201
+ if (!date1) return null
202
+ const diffMs = toDate(date1) - toDate(date2)
203
+ return formatedDuration(Math.floor(Math.abs(diffMs) / 1000), {
204
+ stopFirst: true,
205
+ useFullLabel: true,
206
+ })
207
+ }
208
+
191
209
  const toDate = date => (date instanceof Date ? date : new Date(date))
192
210
  export const toDateFormat = (d, isISO = true) => {
193
211
  if (dateIsPermanent(d)) return '∞'