@osimatic/helpers-js 1.4.27 → 1.4.28
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 +2 -2
- package/number.js +26 -1
- package/package.json +1 -1
- package/tests/number.test.js +45 -0
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/number.js
CHANGED
|
@@ -96,4 +96,29 @@ if (!Number.random) {
|
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
|
|
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
package/tests/number.test.js
CHANGED
|
@@ -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('★★★☆☆');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should round up at 0.5', () => {
|
|
15
|
+
expect(Rating.display(3.5, 5)).toBe('★★★★☆');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('should round down below 0.5', () => {
|
|
19
|
+
expect(Rating.display(3.4, 5)).toBe('★★★☆☆');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('should display max rating', () => {
|
|
23
|
+
expect(Rating.display(5, 5)).toBe('★★★★★');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('should display zero rating', () => {
|
|
27
|
+
expect(Rating.display(0, 5)).toBe('☆☆☆☆☆');
|
|
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, '★', '☆', '½')).toBe('★★★½☆');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('should round up with imgHalf when decimal >= 0.75', () => {
|
|
35
|
+
expect(Rating.display(3.8, 5, '★', '☆', '½')).toBe('★★★★☆');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('should round down with imgHalf when decimal < 0.25', () => {
|
|
39
|
+
expect(Rating.display(3.1, 5, '★', '☆', '½')).toBe('★★★☆☆');
|
|
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');
|