bajo 1.1.11 → 1.1.13

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.
@@ -26,7 +26,13 @@ const defConfig = {
26
26
  date: { dateStyle: 'medium' },
27
27
  time: { timeStyle: 'short' },
28
28
  float: { maximumFractionDigits: 2 },
29
+ double: { maximumFractionDigits: 5 },
30
+ smallint: {},
29
31
  integer: {}
32
+ },
33
+ unitSys: {
34
+ 'en-US': 'imperial',
35
+ id: 'metric'
30
36
  }
31
37
  },
32
38
  exitHandler: true
@@ -21,6 +21,7 @@ import outmatch from 'outmatch'
21
21
  import resolvePath from '../lib/resolve-path.js'
22
22
  import importModule from '../lib/import-module.js'
23
23
  import logLevels from '../lib/log-levels.js'
24
+ import { types as formatTypes, formats } from '../lib/formats.js'
24
25
 
25
26
  const require = createRequire(import.meta.url)
26
27
 
@@ -278,27 +279,56 @@ class BajoCore extends Plugin {
278
279
  return { result, pattern }
279
280
  }
280
281
 
282
+ getUnitFormat = (options = {}) => {
283
+ const lang = options.lang ?? this.config.lang
284
+ let unitSys = options.unitSys ?? this.config.intl.unitSys[lang] ?? 'metric'
285
+ if (!['imperial', 'nautical', 'metric'].includes(unitSys)) unitSys = 'metric'
286
+ return { unitSys, format: formats[unitSys] }
287
+ }
288
+
289
+ formatByType = (type, value, dataType, options = {}) => {
290
+ const { format } = this.getUnitFormat(options)
291
+ const { withUnit = true } = options
292
+ const lang = options.lang ?? this.config.lang
293
+ value = format[`${type}Fn`](value)
294
+ const unit = format[`${type}Unit`]
295
+ const sep = format[`${type}UnitSep`] ?? ' '
296
+ if (!withUnit) return [value, unit, sep]
297
+ const setting = this.defaultsDeep(options[dataType], this.config.intl.format[dataType])
298
+ value = new Intl.NumberFormat(lang, setting).format(value)
299
+ return `${value}${sep}${unit}`
300
+ }
301
+
281
302
  format = (value, type, options = {}) => {
282
303
  const { format } = this.config.intl
283
304
  const { emptyValue = format.emptyValue } = options
284
305
  const lang = options.lang ?? this.config.lang
306
+ options.withUnit = options.withUnit ?? true
307
+ let valueFormatted
285
308
  if ([undefined, null, ''].includes(value)) return emptyValue
286
309
  if (type === 'auto') {
287
310
  if (value instanceof Date) type = 'datetime'
288
311
  }
289
- if (['integer', 'smallint'].includes(type)) {
290
- value = parseInt(value)
312
+ if (['float', 'double'].includes(type) && this.app.bajoSpatial) {
313
+ if (options.latitude) return this.app.bajoSpatial.latToDms(value)
314
+ if (options.longitude) return this.app.bajoSpatial.lngToDms(value)
315
+ }
316
+ if (['integer', 'smallint', 'float', 'double'].includes(type)) {
317
+ value = ['integer', 'smallint'].includes(type) ? parseInt(value) : parseFloat(value)
291
318
  if (isNaN(value)) return emptyValue
319
+ for (const u of formatTypes) {
320
+ if (options[u]) valueFormatted = this.formatByType(u, value, type, options)
321
+ }
322
+ }
323
+ if (['integer', 'smallint'].includes(type)) {
292
324
  const setting = this.defaultsDeep(options.integer, format.integer)
293
- return new Intl.NumberFormat(lang, setting).format(value)
325
+ value = new Intl.NumberFormat(lang, setting).format(Math.round(value))
326
+ return valueFormatted && options.withUnit ? valueFormatted : value
294
327
  }
295
328
  if (['float', 'double'].includes(type)) {
296
- value = parseFloat(value)
297
- if (isNaN(value)) return emptyValue
298
- if (this.app.bajoSpatial && options.latitude) return this.app.bajoSpatial.latToDms(value)
299
- if (this.app.bajoSpatial && options.longitude) return this.app.bajoSpatial.lngToDms(value)
300
- const setting = this.defaultsDeep(options.float, format.float)
301
- return new Intl.NumberFormat(lang, setting).format(value)
329
+ const setting = this.defaultsDeep(options[type], format[type])
330
+ value = new Intl.NumberFormat(lang, setting).format(value)
331
+ return valueFormatted && options.withUnit ? valueFormatted : value
302
332
  }
303
333
  if (['datetime', 'date'].includes(type)) {
304
334
  const setting = this.defaultsDeep(options[type], format[type])
@@ -0,0 +1,37 @@
1
+ export const types = ['speed', 'distance', 'area', 'degree']
2
+
3
+ export const formats = {
4
+ metric: {
5
+ speedFn: (val) => val,
6
+ speedUnit: 'kmh',
7
+ distanceFn: (val) => val,
8
+ distanceUnit: 'km',
9
+ areaFn: (val) => val,
10
+ areaUnit: 'km²',
11
+ degreeFn: (val) => val,
12
+ degreeUnit: '°',
13
+ degreeUnitSep: ''
14
+ },
15
+ imperial: {
16
+ speedFn: (val) => val / 1.609,
17
+ speedUnit: 'mih',
18
+ distanceFn: (val) => val / 1.609,
19
+ distanceUnit: 'mi',
20
+ areaFn: (val) => val / 2.59,
21
+ areaUnit: 'mi²',
22
+ degreeFn: (val) => val,
23
+ degreeUnit: '°',
24
+ degreeUnitSep: ''
25
+ },
26
+ nautical: {
27
+ speedFn: (val) => val / 1.852,
28
+ speedUnit: 'nmh',
29
+ distanceFn: (val) => val / 1.852,
30
+ distanceUnit: 'nm',
31
+ areaFn: (val) => val / 2.92,
32
+ areaUnit: 'nm²',
33
+ degreeFn: (val) => val,
34
+ degreeUnit: '°',
35
+ degreeUnitSep: ''
36
+ }
37
+ }
package/boot/lib/shim.js CHANGED
@@ -8,6 +8,15 @@ function shim () {
8
8
  return this.replace(new RegExp(str, 'g'), newStr)
9
9
  }
10
10
  }
11
+ if (!String.prototype.splice) {
12
+ String.prototype.splice = function(index, count, add) { // eslint-disable-line
13
+ if (index < 0) {
14
+ index += this.length
15
+ if (index < 0) index = 0
16
+ }
17
+ return this.slice(0, index) + (add || '') + this.slice(index + count)
18
+ }
19
+ }
11
20
  }
12
21
 
13
22
  export default shim
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {