summaryrefslogtreecommitdiffstats
path: root/core/src/OC/util.js
blob: 8a50808b5cbf5921249d357e673293cef5e5973e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @license AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import moment from 'moment'

import History from './util-history.js'
import OC from './index.js'
import { formatFileSize as humanFileSize } from '@nextcloud/files'

/**
 * @param {any} t -
 */
function chunkify(t) {
	// Adapted from http://my.opera.com/GreyWyvern/blog/show.dml/1671288
	const tz = []
	let x = 0
	let y = -1
	let n = 0
	let c

	while (x < t.length) {
		c = t.charAt(x)
		// only include the dot in strings
		const m = ((!n && c === '.') || (c >= '0' && c <= '9'))
		if (m !== n) {
			// next chunk
			y++
			tz[y] = ''
			n = m
		}
		tz[y] += c
		x++
	}
	return tz
}

/**
 * Utility functions
 *
 * @namespace OC.Util
 */
export default {

	History,

	/**
	 * @deprecated use https://nextcloud.github.io/nextcloud-files/functions/formatFileSize.html
	 */
	humanFileSize,

	/**
	 * Returns a file size in bytes from a humanly readable string
	 * Makes 2kB to 2048.
	 * Inspired by computerFileSize in helper.php
	 *
	 * @param  {string} string file size in human-readable format
	 * @return {number} or null if string could not be parsed
	 *
	 *
	 */
	computerFileSize(string) {
		if (typeof string !== 'string') {
			return null
		}

		const s = string.toLowerCase().trim()
		let bytes = null

		const bytesArray = {
			b: 1,
			k: 1024,
			kb: 1024,
			mb: 1024 * 1024,
			m: 1024 * 1024,
			gb: 1024 * 1024 * 1024,
			g: 1024 * 1024 * 1024,
			tb: 1024 * 1024 * 1024 * 1024,
			t: 1024 * 1024 * 1024 * 1024,
			pb: 1024 * 1024 * 1024 * 1024 * 1024,
			p: 1024 * 1024 * 1024 * 1024 * 1024,
		}

		const matches = s.match(/^[\s+]?([0-9]*)(\.([0-9]+))?( +)?([kmgtp]?b?)$/i)
		if (matches !== null) {
			bytes = parseFloat(s)
			if (!isFinite(bytes)) {
				return null
			}
		} else {
			return null
		}
		if (matches[5]) {
			bytes = bytes * bytesArray[matches[5]]
		}

		bytes = Math.round(bytes)
		return bytes
	},

	/**
	 * @param {string|number} timestamp timestamp
	 * @param {string} format date format, see momentjs docs
	 * @return {string} timestamp formatted as requested
	 */
	formatDate(timestamp, format) {
		if (window.TESTING === undefined) {
			console.warn('OC.Util.formatDate is deprecated and will be removed in Nextcloud 21. See @nextcloud/moment')
		}
		format = format || 'LLL'
		return moment(timestamp).format(format)
	},

	/**
	 * @param {string|number} timestamp timestamp
	 * @return {string} human readable difference from now
	 */
	relativeModifiedDate(timestamp) {
		if (window.TESTING === undefined) {
			console.warn('OC.Util.relativeModifiedDate is deprecated and will be removed in Nextcloud 21. See @nextcloud/moment')
		}
		const diff = moment().diff(moment(timestamp))
		if (diff >= 0 && diff < 45000) {
			return t('core', 'seconds ago')
		}
		return moment(timestamp).fromNow()
	},

	/**
	 * Returns the width of a generic browser scrollbar
	 *
	 * @return {number} width of scrollbar
	 */
	getScrollBarWidth() {
		if (this._scrollBarWidth) {
			return this._scrollBarWidth
		}

		const inner = document.createElement('p')
		inner.style.width = '100%'
		inner.style.height = '200px'

		const outer = document.createElement('div')
		outer.style.position = 'absolute'
		outer.style.top = '0px'
		outer.style.left = '0px'
		outer.style.visibility = 'hidden'
		outer.style.width = '200px'
		outer.style.height = '150px'
		outer.style.overflow = 'hidden'
		outer.appendChild(inner)

		document.body.appendChild(outer)
		const w1 = inner.offsetWidth
		outer.style.overflow = 'scroll'
		let w2 = inner.offsetWidth
		if (w1 === w2) {
			w2 = outer.clientWidth
		}

		document.body.removeChild(outer)

		this._scrollBarWidth = (w1 - w2)

		return this._scrollBarWidth
	},

	/**
	 * Remove the time component from a given date
	 *
	 * @param {Date} date date
	 * @return {Date} date with stripped time
	 */
	stripTime(date) {
		// FIXME: likely to break when crossing DST
		// would be better to use a library like momentJS
		return new Date(date.getFullYear(), date.getMonth(), date.getDate())
	},

	/**
	 * Compare two strings to provide a natural sort
	 *
	 * @param {string} a first string to compare
	 * @param {string} b second string to compare
	 * @return {number} -1 if b comes before a, 1 if a comes before b
	 * or 0 if the strings are identical
	 */
	naturalSortCompare(a, b) {
		let x
		const aa = chunkify(a)
		const bb = chunkify(b)

		for (x = 0; aa[x] && bb[x]; x++) {
			if (aa[x] !== bb[x]) {
				const aNum = Number(aa[x]); const bNum = Number(bb[x])
				// note: == is correct here
				/* eslint-disable-next-line */
				if (aNum == aa[x] && bNum == bb[x]) {
					return aNum - bNum
				} else {
					// Note: This locale setting isn't supported by all browsers but for the ones
					// that do there will be more consistency between client-server sorting
					return aa[x].localeCompare(bb[x], OC.getLanguage())
				}
			}
		}
		return aa.length - bb.length
	},

	/**
	 * Calls the callback in a given interval until it returns true
	 *
	 * @param {Function} callback function to call on success
	 * @param {number} interval in milliseconds
	 */
	waitFor(callback, interval) {
		const internalCallback = function() {
			if (callback() !== true) {
				setTimeout(internalCallback, interval)
			}
		}

		internalCallback()
	},

	/**
	 * Checks if a cookie with the given name is present and is set to the provided value.
	 *
	 * @param {string} name name of the cookie
	 * @param {string} value value of the cookie
	 * @return {boolean} true if the cookie with the given name has the given value
	 */
	isCookieSetToValue(name, value) {
		const cookies = document.cookie.split(';')
		for (let i = 0; i < cookies.length; i++) {
			const cookie = cookies[i].split('=')
			if (cookie[0].trim() === name && cookie[1].trim() === value) {
				return true
			}
		}
		return false
	},
}