@jamesrock/rockjs 1.0.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.
- package/README.md +5 -0
- package/index.js +128 -0
- package/package.json +12 -0
package/index.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const timeToMinutes = (time) => Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
|
|
2
|
+
const timeToSeconds = (time) => Math.floor((time % (1000 * 60)) / 1000);
|
|
3
|
+
const pad = (time) => time.toString().padStart(2, '0');
|
|
4
|
+
const formatter = new Intl.NumberFormat('en-GB');
|
|
5
|
+
|
|
6
|
+
export const random = (min, max) => (Math.floor(Math.random()*((max-min)+1))+min);
|
|
7
|
+
export const randomIndex = (a) => random(0, a.length-1);
|
|
8
|
+
export const pluckRandom = (a) => a.splice(randomIndex(a), 1)[0];
|
|
9
|
+
export const pluckFirst = (a) => a.splice(0, 1)[0];
|
|
10
|
+
export const pluckLast = (a) => a.splice(a.length-1, 1)[0];
|
|
11
|
+
export const getRandom = (a) => a[randomIndex(a)];
|
|
12
|
+
export const isLandscape = () => window.matchMedia('(orientation: landscape)').matches;
|
|
13
|
+
export const isTiny = () => !window.matchMedia('(min-width: 450px)').matches;
|
|
14
|
+
export const timeToDisplay = (time) => `${pad(timeToMinutes(time))}:${pad(timeToSeconds(time))}`;
|
|
15
|
+
export const makeEven = (value) => value % 2 === 1 ? value - 1 : value;
|
|
16
|
+
export const limit = (value, max) => value > max ? max : value;
|
|
17
|
+
export const formatNumber = (n) => formatter.format(n);
|
|
18
|
+
export const shuffle = (array) => {
|
|
19
|
+
for(let i = array.length - 1; i > 0; i--) {
|
|
20
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
21
|
+
const temp = array[i];
|
|
22
|
+
array[i] = array[j];
|
|
23
|
+
array[j] = temp;
|
|
24
|
+
};
|
|
25
|
+
return array;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const createNode = (type, className) => {
|
|
29
|
+
const node = document.createElement(type);
|
|
30
|
+
if(className) {
|
|
31
|
+
node.classList.add(className);
|
|
32
|
+
};
|
|
33
|
+
return node;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const createButton = (label = '{label}', className) => {
|
|
37
|
+
const btn = createNode('button', className);
|
|
38
|
+
btn.innerText = label;
|
|
39
|
+
return btn;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const createInput = (value = 0, type = 'number') => {
|
|
43
|
+
const input = document.createElement('input');
|
|
44
|
+
input.type = type;
|
|
45
|
+
input.value = value;
|
|
46
|
+
return input;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const createOutput = () => {
|
|
50
|
+
const textarea = document.createElement('textarea');
|
|
51
|
+
textarea.rows = 7;
|
|
52
|
+
return textarea;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const createContainer = (className = '') => {
|
|
56
|
+
return createNode('div', className);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const createSelect = (options) => {
|
|
60
|
+
const node = document.createElement('select');
|
|
61
|
+
options.forEach(([label, value]) => {
|
|
62
|
+
const option = createOption(label, value);
|
|
63
|
+
node.appendChild(option);
|
|
64
|
+
})
|
|
65
|
+
return node;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const createOption = (label = '', value = '') => {
|
|
69
|
+
const node = document.createElement('option');
|
|
70
|
+
node.innerText = label;
|
|
71
|
+
node.value = value;
|
|
72
|
+
return node;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export class Storage {
|
|
76
|
+
constructor(namespace) {
|
|
77
|
+
|
|
78
|
+
this.namespace = namespace;
|
|
79
|
+
|
|
80
|
+
};
|
|
81
|
+
get(key) {
|
|
82
|
+
|
|
83
|
+
var
|
|
84
|
+
existing = this.fetch();
|
|
85
|
+
|
|
86
|
+
return existing[key];
|
|
87
|
+
|
|
88
|
+
};
|
|
89
|
+
set(key, value) {
|
|
90
|
+
|
|
91
|
+
var
|
|
92
|
+
existing = this.fetch();
|
|
93
|
+
|
|
94
|
+
existing[key] = value;
|
|
95
|
+
|
|
96
|
+
this.commit(existing);
|
|
97
|
+
|
|
98
|
+
return this;
|
|
99
|
+
|
|
100
|
+
};
|
|
101
|
+
remove(key) {
|
|
102
|
+
|
|
103
|
+
var
|
|
104
|
+
existing = this.fetch();
|
|
105
|
+
|
|
106
|
+
delete existing[key];
|
|
107
|
+
|
|
108
|
+
this.commit(existing);
|
|
109
|
+
|
|
110
|
+
return this;
|
|
111
|
+
|
|
112
|
+
};
|
|
113
|
+
fetch() {
|
|
114
|
+
|
|
115
|
+
return JSON.parse(localStorage.getItem(this.namespace)||"{}");
|
|
116
|
+
|
|
117
|
+
};
|
|
118
|
+
clear() {
|
|
119
|
+
|
|
120
|
+
this.commit({});
|
|
121
|
+
|
|
122
|
+
};
|
|
123
|
+
commit(obj) {
|
|
124
|
+
|
|
125
|
+
localStorage.setItem(this.namespace, JSON.stringify(obj));
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jamesrock/rockjs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "utility bliss",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "James Rock",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
}
|
|
12
|
+
}
|