@jamesrock/rockjs 1.2.0 → 1.4.0

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 (2) hide show
  1. package/index.js +40 -12
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,7 +1,8 @@
1
- const timeToMinutes = (time) => Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
2
- const timeToSeconds = (time) => Math.floor((time % (1000 * 60)) / 1000);
1
+ const formatMinutes = (time) => Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
2
+ const formatSeconds = (time) => Math.floor((time % (1000 * 60)) / 1000);
3
3
  const pad = (time) => time.toString().padStart(2, '0');
4
4
  const formatter = new Intl.NumberFormat('en-GB');
5
+ const currencyFormatter = new Intl.NumberFormat('en-GB', {style: 'currency', currency: 'GBP'});
5
6
 
6
7
  export const random = (min, max) => (Math.floor(Math.random()*((max-min)+1))+min);
7
8
  export const randomIndex = (a) => random(0, a.length-1);
@@ -11,10 +12,11 @@ export const pluckLast = (a) => a.splice(a.length-1, 1)[0];
11
12
  export const getRandom = (a) => a[randomIndex(a)];
12
13
  export const isLandscape = () => window.matchMedia('(orientation: landscape)').matches;
13
14
  export const isTiny = () => !window.matchMedia('(min-width: 450px)').matches;
14
- export const timeToDisplay = (time) => `${pad(timeToMinutes(time))}:${pad(timeToSeconds(time))}`;
15
15
  export const makeEven = (value) => value % 2 === 1 ? value - 1 : value;
16
16
  export const limit = (value, max) => value > max ? max : value;
17
+ export const formatTime = (t) => `${pad(formatMinutes(t))}:${pad(formatSeconds(t))}`;
17
18
  export const formatNumber = (n) => formatter.format(n);
19
+ export const formatCurrency = (n) => currencyFormatter.format(n);
18
20
  export const shuffle = (array) => {
19
21
  for(let i = array.length - 1; i > 0; i--) {
20
22
  const j = Math.floor(Math.random() * (i + 1));
@@ -25,6 +27,10 @@ export const shuffle = (array) => {
25
27
  return array;
26
28
  };
27
29
 
30
+ export const makeArray = (length, mapper = (a, i) => i) => {
31
+ return Array.from({length}, mapper);
32
+ };
33
+
28
34
  export const createNode = (type, className) => {
29
35
  const node = document.createElement(type);
30
36
  if(className) {
@@ -40,24 +46,24 @@ export const createButton = (label = '{label}', className) => {
40
46
  };
41
47
 
42
48
  export const createInput = (value = 0, type = 'number') => {
43
- const input = document.createElement('input');
49
+ const input = createNode('input');
44
50
  input.type = type;
45
51
  input.value = value;
46
52
  return input;
47
53
  };
48
54
 
49
- export const createOutput = () => {
50
- const textarea = document.createElement('textarea');
51
- textarea.rows = 7;
55
+ export const createOutput = (rows = 7) => {
56
+ const textarea = createNode('textarea');
57
+ textarea.rows = rows;
52
58
  return textarea;
53
59
  };
54
60
 
55
- export const createContainer = (className = '') => {
61
+ export const createContainer = (className) => {
56
62
  return createNode('div', className);
57
63
  };
58
64
 
59
65
  export const createSelect = (options) => {
60
- const node = document.createElement('select');
66
+ const node = createNode('select');
61
67
  options.forEach(([label, value]) => {
62
68
  const option = createOption(label, value);
63
69
  node.appendChild(option);
@@ -65,8 +71,8 @@ export const createSelect = (options) => {
65
71
  return node;
66
72
  };
67
73
 
68
- const createOption = (label = '', value = '') => {
69
- const node = document.createElement('option');
74
+ const createOption = (label = '{label}', value = 0) => {
75
+ const node = createNode('option');
70
76
  node.innerText = label;
71
77
  node.value = value;
72
78
  return node;
@@ -112,7 +118,7 @@ export class Storage {
112
118
  };
113
119
  fetch() {
114
120
 
115
- return JSON.parse(localStorage.getItem(this.namespace)||"{}");
121
+ return JSON.parse(localStorage.getItem(this.namespace)||'{}');
116
122
 
117
123
  };
118
124
  clear() {
@@ -126,3 +132,25 @@ export class Storage {
126
132
 
127
133
  };
128
134
  };
135
+
136
+ export class GUID {
137
+ constructor() {
138
+
139
+ this.chars = [...this.uppercase, ...this.lowercase, ...this.numeric];
140
+
141
+ };
142
+ uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
143
+ lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
144
+ numeric = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
145
+ segment() {
146
+
147
+ return makeArray(4, (a, i) => getRandom(i > 0 ? this.chars : this.uppercase)).join('');
148
+
149
+ };
150
+ get() {
151
+
152
+ return makeArray(this.segments, () => this.segment()).join('-');
153
+
154
+ };
155
+ segments = 3;
156
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",