@osimatic/helpers-js 1.4.27 → 1.4.29

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.
package/index.js CHANGED
@@ -21,7 +21,7 @@ const { HexColor, RgbColor } = require('./draw');
21
21
  const { SocialNetwork } = require('./social_network');
22
22
  const { sleep, refresh } = require('./util');
23
23
  const { chr, ord, trim, empty } = require('./php.min');
24
- const { NumberFormatter } = require('./number');
24
+ const { NumberFormatter, Rating } = require('./number');
25
25
  const { Password } = require('./user');
26
26
 
27
27
  // exports plugins "maison"
@@ -52,7 +52,7 @@ const { WebSocket } = require('./web_socket');
52
52
 
53
53
  module.exports = {
54
54
  Array, Object, Number, String,
55
- HTTPClient, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, VideoMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, DatePeriod, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, Polygon, HexColor, RgbColor, SocialNetwork, NumberFormatter, Password,
55
+ HTTPClient, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, VideoMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, DatePeriod, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, Polygon, HexColor, RgbColor, SocialNetwork, NumberFormatter, Rating, Password,
56
56
  Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, MultipleActionInDivList, MultiFilesInput, EditValue, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ApiTokenSession, ListBox, WebRTC, WebSocket, EventBus,
57
57
  sleep, refresh, chr, ord, trim, empty,
58
58
  Chartjs, GoogleCharts, GoogleRecaptcha, GoogleMap, OpenStreetMap
package/location.js CHANGED
@@ -1,4 +1,7 @@
1
1
 
2
+ const Address = require('ilib/lib/Address');
3
+ const AddressFmt = require('ilib/lib/AddressFmt');
4
+
2
5
  class Country {
3
6
  static setFlagsPath(flagsPath) {
4
7
  Country.flagsPath = flagsPath;
@@ -356,9 +359,6 @@ class PostalAddress {
356
359
  addressDataForPluging['locality'] = addressData['stateDistrict'];
357
360
  }
358
361
 
359
- //var Address = require("ilib/js/lib/Address");
360
- //var AddressFmt = require("ilib/lib/AddressFmt");
361
-
362
362
  let af = new AddressFmt();
363
363
  let formattedAddress = af.format(new Address(addressDataForPluging));
364
364
  return formattedAddress.replace(/\n+/g, separator);
package/number.js CHANGED
@@ -96,4 +96,29 @@ if (!Number.random) {
96
96
  };
97
97
  }
98
98
 
99
- module.exports = { NumberFormatter };
99
+ class Rating {
100
+ static display(rating, maxRating = 5, imgOn = '★', imgOff = '☆', imgHalf = null) {
101
+ if (rating == null) {
102
+ return '';
103
+ }
104
+
105
+ const fullStars = Math.floor(rating);
106
+ const decimal = rating - fullStars;
107
+
108
+ let totalFull = fullStars;
109
+ let showHalf = false;
110
+
111
+ if (imgHalf !== null && decimal >= 0.25 && decimal < 0.75) {
112
+ showHalf = true;
113
+ }
114
+ else if (decimal >= (imgHalf !== null ? 0.75 : 0.5)) {
115
+ totalFull++;
116
+ }
117
+
118
+ return imgOn.repeat(totalFull)
119
+ + (showHalf ? imgHalf : '')
120
+ + imgOff.repeat(maxRating - totalFull - (showHalf ? 1 : 0));
121
+ }
122
+ }
123
+
124
+ module.exports = { NumberFormatter, Rating };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.4.27",
3
+ "version": "1.4.29",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -13,7 +13,7 @@
13
13
  "license": "ISC",
14
14
  "description": "",
15
15
  "dependencies": {
16
- "ilib": "^14.16.0"
16
+ "ilib": "^14.21"
17
17
  },
18
18
  "devDependencies": {
19
19
  "jest": "^30.2.0",
@@ -1,4 +1,4 @@
1
- const { Country, GeographicCoordinates, Polygon } = require('../location');
1
+ const { Country, PostalAddress, GeographicCoordinates, Polygon } = require('../location');
2
2
 
3
3
  describe('Country', () => {
4
4
  describe('getCountries', () => {
@@ -592,4 +592,119 @@ describe('Polygon', () => {
592
592
  expect(lastPoint).toEqual(firstPoint);
593
593
  });
594
594
  });
595
+ });
596
+
597
+ describe('PostalAddress', () => {
598
+ describe('format', () => {
599
+ test('should return a string', () => {
600
+ const result = PostalAddress.format({
601
+ streetAddress: '10 Rue de la Paix',
602
+ postalCode: '75001',
603
+ locality: 'Paris',
604
+ countryCode: 'FR',
605
+ });
606
+ expect(typeof result).toBe('string');
607
+ });
608
+
609
+ test('should contain streetAddress, postalCode and locality', () => {
610
+ const result = PostalAddress.format({
611
+ streetAddress: '10 Rue de la Paix',
612
+ postalCode: '75001',
613
+ locality: 'Paris',
614
+ countryCode: 'FR',
615
+ });
616
+ expect(result).toContain('10 Rue de la Paix');
617
+ expect(result).toContain('75001');
618
+ expect(result).toContain('Paris');
619
+ });
620
+
621
+ test('should use <br/> as default separator', () => {
622
+ const result = PostalAddress.format({
623
+ streetAddress: '10 Rue de la Paix',
624
+ postalCode: '75001',
625
+ locality: 'Paris',
626
+ countryCode: 'FR',
627
+ });
628
+ expect(result).toContain('<br/>');
629
+ expect(result).not.toContain('\n');
630
+ });
631
+
632
+ test('should use custom separator when provided', () => {
633
+ const result = PostalAddress.format({
634
+ streetAddress: '10 Rue de la Paix',
635
+ postalCode: '75001',
636
+ locality: 'Paris',
637
+ countryCode: 'FR',
638
+ }, ', ');
639
+ expect(result).toContain(', ');
640
+ expect(result).not.toContain('\n');
641
+ });
642
+
643
+ test('should include additionalAddress on a separate line', () => {
644
+ const result = PostalAddress.format({
645
+ streetAddress: '10 Rue de la Paix',
646
+ additionalAddress: 'Bât. B',
647
+ postalCode: '75001',
648
+ locality: 'Paris',
649
+ countryCode: 'FR',
650
+ });
651
+ expect(result).toContain('10 Rue de la Paix');
652
+ expect(result).toContain('Bât. B');
653
+ });
654
+
655
+ test('should use suburb as locality fallback when locality is null', () => {
656
+ const result = PostalAddress.format({
657
+ streetAddress: '5 High Street',
658
+ postalCode: 'SW1A 1AA',
659
+ suburb: 'Westminster',
660
+ countryCode: 'GB',
661
+ });
662
+ expect(result).toContain('Westminster');
663
+ });
664
+
665
+ test('should use stateDistrict as locality fallback when locality and suburb are null', () => {
666
+ const result = PostalAddress.format({
667
+ streetAddress: '5 High Street',
668
+ postalCode: 'SW1A 1AA',
669
+ stateDistrict: 'Greater London',
670
+ countryCode: 'GB',
671
+ });
672
+ expect(result).toContain('Greater London');
673
+ });
674
+
675
+ test('should handle null/undefined optional fields without throwing', () => {
676
+ expect(() => PostalAddress.format({
677
+ streetAddress: null,
678
+ additionalAddress: null,
679
+ postalCode: null,
680
+ locality: null,
681
+ state: null,
682
+ countryCode: null,
683
+ })).not.toThrow();
684
+ });
685
+
686
+ test('should handle empty strings for optional fields', () => {
687
+ expect(() => PostalAddress.format({
688
+ streetAddress: '',
689
+ additionalAddress: '',
690
+ postalCode: '',
691
+ locality: '',
692
+ state: '',
693
+ countryCode: '',
694
+ })).not.toThrow();
695
+ });
696
+
697
+ test('should include state/region in the formatted address', () => {
698
+ const result = PostalAddress.format({
699
+ streetAddress: '1600 Amphitheatre Pkwy',
700
+ locality: 'Mountain View',
701
+ state: 'CA',
702
+ postalCode: '94043',
703
+ countryCode: 'US',
704
+ });
705
+ expect(result).toContain('CA');
706
+ expect(result).toContain('Mountain View');
707
+ expect(result).toContain('94043');
708
+ });
709
+ });
595
710
  });
@@ -1,4 +1,49 @@
1
1
  require('../number');
2
+ const { Rating } = require('../number');
3
+
4
+ describe('Rating', () => {
5
+ describe('display', () => {
6
+ test('should return empty string for null rating', () => {
7
+ expect(Rating.display(null)).toBe('');
8
+ });
9
+
10
+ test('should display full stars only', () => {
11
+ expect(Rating.display(3, 5)).toBe('&#9733;&#9733;&#9733;&#9734;&#9734;');
12
+ });
13
+
14
+ test('should round up at 0.5', () => {
15
+ expect(Rating.display(3.5, 5)).toBe('&#9733;&#9733;&#9733;&#9733;&#9734;');
16
+ });
17
+
18
+ test('should round down below 0.5', () => {
19
+ expect(Rating.display(3.4, 5)).toBe('&#9733;&#9733;&#9733;&#9734;&#9734;');
20
+ });
21
+
22
+ test('should display max rating', () => {
23
+ expect(Rating.display(5, 5)).toBe('&#9733;&#9733;&#9733;&#9733;&#9733;');
24
+ });
25
+
26
+ test('should display zero rating', () => {
27
+ expect(Rating.display(0, 5)).toBe('&#9734;&#9734;&#9734;&#9734;&#9734;');
28
+ });
29
+
30
+ test('should show half star when imgHalf provided and decimal in [0.25, 0.75)', () => {
31
+ expect(Rating.display(3.5, 5, '&#9733;', '&#9734;', '½')).toBe('&#9733;&#9733;&#9733;½&#9734;');
32
+ });
33
+
34
+ test('should round up with imgHalf when decimal >= 0.75', () => {
35
+ expect(Rating.display(3.8, 5, '&#9733;', '&#9734;', '½')).toBe('&#9733;&#9733;&#9733;&#9733;&#9734;');
36
+ });
37
+
38
+ test('should round down with imgHalf when decimal < 0.25', () => {
39
+ expect(Rating.display(3.1, 5, '&#9733;', '&#9734;', '½')).toBe('&#9733;&#9733;&#9733;&#9734;&#9734;');
40
+ });
41
+
42
+ test('should support custom image strings', () => {
43
+ expect(Rating.display(2, 3, '[on]', '[off]')).toBe('[on][on][off]');
44
+ });
45
+ });
46
+ });
2
47
 
3
48
  describe('NumberFormatter', () => {
4
49
  const { NumberFormatter } = require('../number');