| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const assert = require('assert'); | 
					
						
							| 
									
										
										
										
											2023-04-25 16:48:16 -04:00
										 |  |  | const validator = require('validator'); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | const { JSDOM } = require('jsdom'); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const slugify = require('../src/slugify'); | 
					
						
							| 
									
										
										
										
											2019-07-09 22:00:46 -04:00
										 |  |  | const db = require('./mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('Utility Methods', () => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 	// https://gist.github.com/robballou/9ee108758dc5e0e2d028
 | 
					
						
							|  |  |  | 	// create some jsdom magic to allow jQuery to work
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	const dom = new JSDOM('<html><body></body></html>'); | 
					
						
							| 
									
										
											  
											
												Webpack5 (#10311)
* feat: webpack 5 part 1
* fix: gruntfile fixes
* fix: fix taskbar warning
add app.importScript
copy public/src/modules to build folder
* refactor: remove commented old code
* feat: reenable admin
* fix: acp settings pages, fix sortable on manage categories
embedded require in html not allowed
* fix: bundle serialize/deserizeli so plugins dont break
* test: fixe util tests
* test: fix require path
* test: more test fixes
* test: require correct utils module
* test: require correct utils
* test: log stack
* test: fix db require blowing up tests
* test: move and disable bundle test
* refactor: add aliases
* test: disable testing route
* fix: move webpack modules necessary for build, into `dependencies`
* test: fix one more test
remove 500-embed.tpl
* fix: restore use of assets/nodebb.min.js, at least for now
* fix: remove unnecessary line break
* fix: point to proper ACP bundle
* test: maybe fix build test
* test: composer
* refactor: dont need dist
* refactor: more cleanup
use everything from build/public folder
* get rid of conditional import in app.js
* fix: ace
* refactor: cropper alias
* test: lint and test fixes
* lint: fix
* refactor: rename function to app.require
* refactor: go back to using app.require
* chore: use github branch
* chore: use webpack branch
* feat: webpack webinstaller
* feat: add chunkFile name with contenthash
* refactor: move hooks to top
* refactor: get rid of template500Function
* fix(deps): use webpack5 branch of 2factor plugin
* chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket:
* refactor: disable cache on templates
loadTemplate is called once by benchpress and the result is cache internally
* refactor: add server side helpers.js
* feat: deprecate /plugins shorthand route, closes #10343
* refactor: use build/public for webpack
* test: fix filename
* fix: more specific selector
* lint: ignore
* refactor: fix comments
* test: add debug for random failing test
* refactor: cleanup
remove test page, remove dupe functions in utils.common
* lint: use relative path  for now
* chore: bump prerelease version
* feat: add translateKeys
* fix: optional params
* fix: get rid of extra timeago files
* refactor: cleanup, require timeago locale earlier
remove translator.prepareDOM, it is in header.tpl html tag
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* feat: allow app.require('bootbox'/'benchpressjs')
* refactor: require server side utils
* test: jquery ready
* change istaller to use build/public
* test: use document.addEventListener
* refactor: closes #10301
* refactor: generateTopicClass
* fix: column counts for other privileges
* fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking]
* fix: typo in hook name
* refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags)
* fix: crash if `delay` not passed in (as it cannot be destructured)
* refactor: replace substr
* feat: set --panel-offset style in html element based on stored value in localStorage
* refactor: addDropupHandler() logic to be less naive
- Take into account height of the menu
- Don't apply dropUp logic if there's nothing in the dropdown
- Remove 'hidden' class (added by default in Persona for post tools) when menu items are added
closes #10423
* refactor: simplify utils.params [breaking]
Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do.
* feat: add support for returning full URLSearchParams for utils.params
* fix: utils.params() fallback handling
* fix: default empty obj for params()
* fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end
* fix: utils.params() not allowing relative paths to be passed in
* refactor(DRY): new assertPasswordValidity utils method
* fix: incorrect error message returned on insufficient privilege on flag edit
* fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate
- added failing tests and patched up middleware.assert.flags to fix
* refactor: flag api v3 tests to create new post and flags on every round
* fix: missing error:no-flag language key
* refactor: flags.canView to check flag existence, simplify middleware.assert.flag
* feat: flag deletion API endpoint, #10426
* feat: UI for flag deletion, closes #10426
* chore: update plugin versions
* chore: up emoji
* chore: update markdown
* chore: up emoji-android
* fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check
Co-authored-by: Julian Lam <julian@nodebb.org>
											
										 
											2022-04-29 21:39:33 -04:00
										 |  |  | 	global.window = dom.window; | 
					
						
							|  |  |  | 	global.document = dom.window.document; | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 	global.jQuery = require('jquery'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 	global.$ = global.jQuery; | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 	const { $ } = global; | 
					
						
							| 
									
										
											  
											
												Webpack5 (#10311)
* feat: webpack 5 part 1
* fix: gruntfile fixes
* fix: fix taskbar warning
add app.importScript
copy public/src/modules to build folder
* refactor: remove commented old code
* feat: reenable admin
* fix: acp settings pages, fix sortable on manage categories
embedded require in html not allowed
* fix: bundle serialize/deserizeli so plugins dont break
* test: fixe util tests
* test: fix require path
* test: more test fixes
* test: require correct utils module
* test: require correct utils
* test: log stack
* test: fix db require blowing up tests
* test: move and disable bundle test
* refactor: add aliases
* test: disable testing route
* fix: move webpack modules necessary for build, into `dependencies`
* test: fix one more test
remove 500-embed.tpl
* fix: restore use of assets/nodebb.min.js, at least for now
* fix: remove unnecessary line break
* fix: point to proper ACP bundle
* test: maybe fix build test
* test: composer
* refactor: dont need dist
* refactor: more cleanup
use everything from build/public folder
* get rid of conditional import in app.js
* fix: ace
* refactor: cropper alias
* test: lint and test fixes
* lint: fix
* refactor: rename function to app.require
* refactor: go back to using app.require
* chore: use github branch
* chore: use webpack branch
* feat: webpack webinstaller
* feat: add chunkFile name with contenthash
* refactor: move hooks to top
* refactor: get rid of template500Function
* fix(deps): use webpack5 branch of 2factor plugin
* chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket:
* refactor: disable cache on templates
loadTemplate is called once by benchpress and the result is cache internally
* refactor: add server side helpers.js
* feat: deprecate /plugins shorthand route, closes #10343
* refactor: use build/public for webpack
* test: fix filename
* fix: more specific selector
* lint: ignore
* refactor: fix comments
* test: add debug for random failing test
* refactor: cleanup
remove test page, remove dupe functions in utils.common
* lint: use relative path  for now
* chore: bump prerelease version
* feat: add translateKeys
* fix: optional params
* fix: get rid of extra timeago files
* refactor: cleanup, require timeago locale earlier
remove translator.prepareDOM, it is in header.tpl html tag
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* feat: allow app.require('bootbox'/'benchpressjs')
* refactor: require server side utils
* test: jquery ready
* change istaller to use build/public
* test: use document.addEventListener
* refactor: closes #10301
* refactor: generateTopicClass
* fix: column counts for other privileges
* fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking]
* fix: typo in hook name
* refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags)
* fix: crash if `delay` not passed in (as it cannot be destructured)
* refactor: replace substr
* feat: set --panel-offset style in html element based on stored value in localStorage
* refactor: addDropupHandler() logic to be less naive
- Take into account height of the menu
- Don't apply dropUp logic if there's nothing in the dropdown
- Remove 'hidden' class (added by default in Persona for post tools) when menu items are added
closes #10423
* refactor: simplify utils.params [breaking]
Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do.
* feat: add support for returning full URLSearchParams for utils.params
* fix: utils.params() fallback handling
* fix: default empty obj for params()
* fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end
* fix: utils.params() not allowing relative paths to be passed in
* refactor(DRY): new assertPasswordValidity utils method
* fix: incorrect error message returned on insufficient privilege on flag edit
* fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate
- added failing tests and patched up middleware.assert.flags to fix
* refactor: flag api v3 tests to create new post and flags on every round
* fix: missing error:no-flag language key
* refactor: flags.canView to check flag existence, simplify middleware.assert.flag
* feat: flag deletion API endpoint, #10426
* feat: UI for flag deletion, closes #10426
* chore: update plugin versions
* chore: up emoji
* chore: update markdown
* chore: up emoji-android
* fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check
Co-authored-by: Julian Lam <julian@nodebb.org>
											
										 
											2022-04-29 21:39:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const utils = require('../public/src/utils'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 	// https://github.com/jprichardson/string.js/blob/master/test/string.test.js
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should decode HTML entities', (done) => { | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 		assert.strictEqual( | 
					
						
							|  |  |  | 			utils.decodeHTMLEntities('Ken Thompson & Dennis Ritchie'), | 
					
						
							|  |  |  | 			'Ken Thompson & Dennis Ritchie' | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		assert.strictEqual( | 
					
						
							|  |  |  | 			utils.decodeHTMLEntities('3 < 4'), | 
					
						
							|  |  |  | 			'3 < 4' | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		assert.strictEqual( | 
					
						
							|  |  |  | 			utils.decodeHTMLEntities('http://'), | 
					
						
							|  |  |  | 			'http://' | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2023-04-25 16:48:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should strip HTML tags', (done) => { | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 		assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>'), 'just some text'); | 
					
						
							|  |  |  | 		assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>', ['p']), 'just <b>some</b> text'); | 
					
						
							| 
									
										
										
										
											2017-11-01 12:05:06 -04:00
										 |  |  | 		assert.strictEqual(utils.stripHTMLTags('<i>just</i> some <image/> text', ['i']), 'just some <image/> text'); | 
					
						
							|  |  |  | 		assert.strictEqual(utils.stripHTMLTags('<i>just</i> some <image/> <div>text</div>', ['i', 'div']), 'just some <image/> text'); | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should preserve case if requested', (done) => { | 
					
						
							| 
									
										
										
										
											2020-10-11 21:49:37 -04:00
										 |  |  | 		assert.strictEqual(slugify('UPPER CASE', true), 'UPPER-CASE'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should work if a number is passed in', (done) => { | 
					
						
							| 
									
										
										
										
											2020-10-11 21:49:37 -04:00
										 |  |  | 		assert.strictEqual(slugify(12345), '12345'); | 
					
						
							| 
									
										
										
										
											2019-10-17 18:55:56 -04:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('username validation', () => { | 
					
						
							|  |  |  | 		it('accepts latin-1 characters', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const username = "John\"'-. Doeäâèéë1234"; | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | 			assert(utils.isUserNameValid(username), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-11-07 16:01:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('rejects empty string', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const username = ''; | 
					
						
							| 
									
										
										
										
											2018-05-25 19:05:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid(username), false, 'accepted as valid username'); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-11-07 16:01:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should reject new lines', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid('myusername\r\n'), false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should reject new lines', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid('myusername\n'), false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should reject tabs', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid('myusername\t'), false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('accepts square brackets', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const username = '[best clan] julian'; | 
					
						
							| 
									
										
										
										
											2018-11-07 16:01:22 -05:00
										 |  |  | 			assert(utils.isUserNameValid(username), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('accepts regular username', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert(utils.isUserNameValid('myusername'), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('accepts quotes', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert(utils.isUserNameValid('baris "the best" usakli'), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('email validation', () => { | 
					
						
							|  |  |  | 		it('accepts sample address', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const email = 'sample@example.com'; | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 			assert(utils.isEmailValid(email), 'invalid email'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('rejects empty address', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const email = ''; | 
					
						
							| 
									
										
										
										
											2018-05-25 19:05:18 -04:00
										 |  |  | 			assert.equal(utils.isEmailValid(email), false, 'accepted as valid email'); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-09 18:18:02 -05:00
										 |  |  | 	describe('UUID generation / secureRandom', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('return unique random value every time', () => { | 
					
						
							| 
									
										
										
										
											2022-05-26 12:42:09 -04:00
										 |  |  | 			delete require.cache[require.resolve('../src/utils')]; | 
					
						
							|  |  |  | 			const { generateUUID } = require('../src/utils'); | 
					
						
							|  |  |  | 			const uuid1 = generateUUID(); | 
					
						
							|  |  |  | 			const uuid2 = generateUUID(); | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 			assert.notEqual(uuid1, uuid2, 'matches'); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2024-12-09 18:18:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should return a random number between 1-10 inclusive', () => { | 
					
						
							|  |  |  | 			const { secureRandom } = require('../src/utils'); | 
					
						
							|  |  |  | 			const r1 = secureRandom(1, 10); | 
					
						
							|  |  |  | 			assert(r1 >= 1); | 
					
						
							|  |  |  | 			assert(r1 <= 10); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should always return 3', () => { | 
					
						
							| 
									
										
										
										
											2024-12-09 18:25:31 -05:00
										 |  |  | 			const { secureRandom } = require('../src/utils'); | 
					
						
							| 
									
										
										
										
											2024-12-09 18:18:02 -05:00
										 |  |  | 			const r1 = secureRandom(3, 3); | 
					
						
							|  |  |  | 			assert.strictEqual(r1, 3); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-14 13:12:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('cleanUpTag', () => { | 
					
						
							|  |  |  | 		it('should cleanUp a tag', (done) => { | 
					
						
							| 
									
										
										
										
											2022-01-10 18:54:06 -05:00
										 |  |  | 			const cleanedTag = utils.cleanUpTag(',/#!$^*;TaG1:{}=_`<>\'"~()?|'); | 
					
						
							| 
									
										
										
										
											2016-10-14 13:12:08 +03:00
										 |  |  | 			assert.equal(cleanedTag, 'tag1'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return empty string for invalid tags', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-14 13:12:08 +03:00
										 |  |  | 			assert.strictEqual(utils.cleanUpTag(undefined), ''); | 
					
						
							|  |  |  | 			assert.strictEqual(utils.cleanUpTag(null), ''); | 
					
						
							|  |  |  | 			assert.strictEqual(utils.cleanUpTag(false), ''); | 
					
						
							|  |  |  | 			assert.strictEqual(utils.cleanUpTag(1), ''); | 
					
						
							|  |  |  | 			assert.strictEqual(utils.cleanUpTag(0), ''); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should remove punctuation', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const removed = utils.removePunctuation('some text with , ! punctuation inside "'); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(removed, 'some text with   punctuation inside '); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-25 16:48:16 -04:00
										 |  |  | 	it('should get language key', () => { | 
					
						
							|  |  |  | 		assert.strictEqual(utils.getLanguage(), 'en-GB'); | 
					
						
							|  |  |  | 		global.window.utils = {}; | 
					
						
							|  |  |  | 		global.window.config = { userLang: 'tr' }; | 
					
						
							|  |  |  | 		assert.strictEqual(utils.getLanguage(), 'tr'); | 
					
						
							|  |  |  | 		global.window.config = { defaultLang: 'de' }; | 
					
						
							|  |  |  | 		assert.strictEqual(utils.getLanguage(), 'de'); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return true if string has language key', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.hasLanguageKey('some text [[topic:title]] and [[user:reputaiton]]'), true); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return false if string does not have language key', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.hasLanguageKey('some text with no language keys'), false); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-25 16:48:16 -04:00
										 |  |  | 	it('should return bootstrap env', () => { | 
					
						
							|  |  |  | 		assert.strictEqual(utils.findBootstrapEnvironment(), 'xs'); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should check if mobile', () => { | 
					
						
							|  |  |  | 		assert.strictEqual(utils.isMobile(), true); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should check password validity', () => { | 
					
						
							|  |  |  | 		global.ajaxify = { | 
					
						
							|  |  |  | 			data: { | 
					
						
							|  |  |  | 				minimumPasswordStrength: 1, | 
					
						
							|  |  |  | 				minimumPasswordLength: 6, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		const zxcvbn = require('zxcvbn'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function check(pwd, expectedError) { | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				utils.assertPasswordValidity(pwd, zxcvbn); | 
					
						
							|  |  |  | 				assert(false); | 
					
						
							|  |  |  | 			} catch (err) { | 
					
						
							|  |  |  | 				assert.strictEqual(err.message, expectedError); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-10-05 12:48:50 -04:00
										 |  |  | 		check('123456', '[[user:weak-password]]'); | 
					
						
							|  |  |  | 		check('', '[[user:change-password-error]]'); | 
					
						
							|  |  |  | 		check('asd', '[[reset_password:password-too-short]]'); | 
					
						
							| 
									
										
										
										
											2023-04-25 16:48:16 -04:00
										 |  |  | 		check(new Array(513).fill('a').join(''), '[[error:password-too-long]]'); | 
					
						
							|  |  |  | 		utils.assertPasswordValidity('Yzsh31j!a', zxcvbn); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 11:35:20 -05:00
										 |  |  | 	it('should generate UUID', () => { | 
					
						
							|  |  |  | 		assert(validator.isUUID(utils.generateUUID())); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2023-04-25 16:48:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should shallow merge two objects', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const a = { foo: 1, cat1: 'ginger' }; | 
					
						
							|  |  |  | 		const b = { baz: 2, cat2: 'phoebe' }; | 
					
						
							|  |  |  | 		const obj = utils.merge(a, b); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.strictEqual(obj.foo, 1); | 
					
						
							|  |  |  | 		assert.strictEqual(obj.baz, 2); | 
					
						
							|  |  |  | 		assert.strictEqual(obj.cat1, 'ginger'); | 
					
						
							|  |  |  | 		assert.strictEqual(obj.cat2, 'phoebe'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return the file extesion', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.fileExtension('/path/to/some/file.png'), 'png'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return file mime type', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.fileMimeType('/path/to/some/file.png'), 'image/png'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should check if url is relative', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.isRelativeUrl('/topic/1/slug'), true); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should check if url is relative', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.isRelativeUrl('https://nodebb.org'), false); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable('1000'), '1.0k'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable('1100000'), '1.1m'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable('100'), '100'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2023-07-19 08:05:22 -04:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable(null), 'null'); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make numbers human readable on elements', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const el = $('<div title="100000"></div>'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		utils.makeNumbersHumanReadable(el); | 
					
						
							|  |  |  | 		assert.equal(el.html(), '100.0k'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.addCommas('100'), '100'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.addCommas('1000'), '1,000'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.addCommas('1000000'), '1,000,000'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to elements', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const el = $('<div>1000000</div>'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		utils.addCommasToNumbers(el); | 
					
						
							|  |  |  | 		assert.equal(el.html(), '1,000,000'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return passed in value if invalid', (done) => { | 
					
						
							| 
									
										
										
										
											2021-11-18 16:42:18 -05:00
										 |  |  | 		// eslint-disable-next-line no-loss-of-precision
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const bigInt = -111111111111111111; | 
					
						
							|  |  |  | 		const result = utils.toISOString(bigInt); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert.equal(bigInt, result); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return false if browser is not android', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		global.navigator = { | 
					
						
							|  |  |  | 			userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36', | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		assert.equal(utils.isAndroidBrowser(), false); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return true if browser is android', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		global.navigator = { | 
					
						
							|  |  |  | 			userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Android /58.0.3029.96 Safari/537.36', | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		assert.equal(utils.isAndroidBrowser(), true); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should check if element is in viewport', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const el = $('<div>some text</div>'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert(utils.isElementInViewport(el)); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get empty object for url params', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const params = utils.params(); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert.equal(Object.keys(params), 0); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get url params', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp=2' }); | 
					
						
							| 
									
										
											  
											
												Webpack5 (#10311)
* feat: webpack 5 part 1
* fix: gruntfile fixes
* fix: fix taskbar warning
add app.importScript
copy public/src/modules to build folder
* refactor: remove commented old code
* feat: reenable admin
* fix: acp settings pages, fix sortable on manage categories
embedded require in html not allowed
* fix: bundle serialize/deserizeli so plugins dont break
* test: fixe util tests
* test: fix require path
* test: more test fixes
* test: require correct utils module
* test: require correct utils
* test: log stack
* test: fix db require blowing up tests
* test: move and disable bundle test
* refactor: add aliases
* test: disable testing route
* fix: move webpack modules necessary for build, into `dependencies`
* test: fix one more test
remove 500-embed.tpl
* fix: restore use of assets/nodebb.min.js, at least for now
* fix: remove unnecessary line break
* fix: point to proper ACP bundle
* test: maybe fix build test
* test: composer
* refactor: dont need dist
* refactor: more cleanup
use everything from build/public folder
* get rid of conditional import in app.js
* fix: ace
* refactor: cropper alias
* test: lint and test fixes
* lint: fix
* refactor: rename function to app.require
* refactor: go back to using app.require
* chore: use github branch
* chore: use webpack branch
* feat: webpack webinstaller
* feat: add chunkFile name with contenthash
* refactor: move hooks to top
* refactor: get rid of template500Function
* fix(deps): use webpack5 branch of 2factor plugin
* chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket:
* refactor: disable cache on templates
loadTemplate is called once by benchpress and the result is cache internally
* refactor: add server side helpers.js
* feat: deprecate /plugins shorthand route, closes #10343
* refactor: use build/public for webpack
* test: fix filename
* fix: more specific selector
* lint: ignore
* refactor: fix comments
* test: add debug for random failing test
* refactor: cleanup
remove test page, remove dupe functions in utils.common
* lint: use relative path  for now
* chore: bump prerelease version
* feat: add translateKeys
* fix: optional params
* fix: get rid of extra timeago files
* refactor: cleanup, require timeago locale earlier
remove translator.prepareDOM, it is in header.tpl html tag
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* feat: allow app.require('bootbox'/'benchpressjs')
* refactor: require server side utils
* test: jquery ready
* change istaller to use build/public
* test: use document.addEventListener
* refactor: closes #10301
* refactor: generateTopicClass
* fix: column counts for other privileges
* fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking]
* fix: typo in hook name
* refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags)
* fix: crash if `delay` not passed in (as it cannot be destructured)
* refactor: replace substr
* feat: set --panel-offset style in html element based on stored value in localStorage
* refactor: addDropupHandler() logic to be less naive
- Take into account height of the menu
- Don't apply dropUp logic if there's nothing in the dropdown
- Remove 'hidden' class (added by default in Persona for post tools) when menu items are added
closes #10423
* refactor: simplify utils.params [breaking]
Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do.
* feat: add support for returning full URLSearchParams for utils.params
* fix: utils.params() fallback handling
* fix: default empty obj for params()
* fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end
* fix: utils.params() not allowing relative paths to be passed in
* refactor(DRY): new assertPasswordValidity utils method
* fix: incorrect error message returned on insufficient privilege on flag edit
* fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate
- added failing tests and patched up middleware.assert.flags to fix
* refactor: flag api v3 tests to create new post and flags on every round
* fix: missing error:no-flag language key
* refactor: flags.canView to check flag existence, simplify middleware.assert.flag
* feat: flag deletion API endpoint, #10426
* feat: UI for flag deletion, closes #10426
* chore: update plugin versions
* chore: up emoji
* chore: update markdown
* chore: up emoji-android
* fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check
Co-authored-by: Julian Lam <julian@nodebb.org>
											
										 
											2022-04-29 21:39:33 -04:00
										 |  |  | 		assert.strictEqual(params.foo, 1); | 
					
						
							|  |  |  | 		assert.strictEqual(params.bar, 'test'); | 
					
						
							|  |  |  | 		assert.strictEqual(params.herp, 2); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:36:18 -05:00
										 |  |  | 	it('should get url params as arrays', (done) => { | 
					
						
							|  |  |  | 		const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3' }); | 
					
						
							| 
									
										
											  
											
												Webpack5 (#10311)
* feat: webpack 5 part 1
* fix: gruntfile fixes
* fix: fix taskbar warning
add app.importScript
copy public/src/modules to build folder
* refactor: remove commented old code
* feat: reenable admin
* fix: acp settings pages, fix sortable on manage categories
embedded require in html not allowed
* fix: bundle serialize/deserizeli so plugins dont break
* test: fixe util tests
* test: fix require path
* test: more test fixes
* test: require correct utils module
* test: require correct utils
* test: log stack
* test: fix db require blowing up tests
* test: move and disable bundle test
* refactor: add aliases
* test: disable testing route
* fix: move webpack modules necessary for build, into `dependencies`
* test: fix one more test
remove 500-embed.tpl
* fix: restore use of assets/nodebb.min.js, at least for now
* fix: remove unnecessary line break
* fix: point to proper ACP bundle
* test: maybe fix build test
* test: composer
* refactor: dont need dist
* refactor: more cleanup
use everything from build/public folder
* get rid of conditional import in app.js
* fix: ace
* refactor: cropper alias
* test: lint and test fixes
* lint: fix
* refactor: rename function to app.require
* refactor: go back to using app.require
* chore: use github branch
* chore: use webpack branch
* feat: webpack webinstaller
* feat: add chunkFile name with contenthash
* refactor: move hooks to top
* refactor: get rid of template500Function
* fix(deps): use webpack5 branch of 2factor plugin
* chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket:
* refactor: disable cache on templates
loadTemplate is called once by benchpress and the result is cache internally
* refactor: add server side helpers.js
* feat: deprecate /plugins shorthand route, closes #10343
* refactor: use build/public for webpack
* test: fix filename
* fix: more specific selector
* lint: ignore
* refactor: fix comments
* test: add debug for random failing test
* refactor: cleanup
remove test page, remove dupe functions in utils.common
* lint: use relative path  for now
* chore: bump prerelease version
* feat: add translateKeys
* fix: optional params
* fix: get rid of extra timeago files
* refactor: cleanup, require timeago locale earlier
remove translator.prepareDOM, it is in header.tpl html tag
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* feat: allow app.require('bootbox'/'benchpressjs')
* refactor: require server side utils
* test: jquery ready
* change istaller to use build/public
* test: use document.addEventListener
* refactor: closes #10301
* refactor: generateTopicClass
* fix: column counts for other privileges
* fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking]
* fix: typo in hook name
* refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags)
* fix: crash if `delay` not passed in (as it cannot be destructured)
* refactor: replace substr
* feat: set --panel-offset style in html element based on stored value in localStorage
* refactor: addDropupHandler() logic to be less naive
- Take into account height of the menu
- Don't apply dropUp logic if there's nothing in the dropdown
- Remove 'hidden' class (added by default in Persona for post tools) when menu items are added
closes #10423
* refactor: simplify utils.params [breaking]
Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do.
* feat: add support for returning full URLSearchParams for utils.params
* fix: utils.params() fallback handling
* fix: default empty obj for params()
* fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end
* fix: utils.params() not allowing relative paths to be passed in
* refactor(DRY): new assertPasswordValidity utils method
* fix: incorrect error message returned on insufficient privilege on flag edit
* fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate
- added failing tests and patched up middleware.assert.flags to fix
* refactor: flag api v3 tests to create new post and flags on every round
* fix: missing error:no-flag language key
* refactor: flags.canView to check flag existence, simplify middleware.assert.flag
* feat: flag deletion API endpoint, #10426
* feat: UI for flag deletion, closes #10426
* chore: update plugin versions
* chore: up emoji
* chore: update markdown
* chore: up emoji-android
* fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check
Co-authored-by: Julian Lam <julian@nodebb.org>
											
										 
											2022-04-29 21:39:33 -04:00
										 |  |  | 		assert.strictEqual(params.foo, 1); | 
					
						
							|  |  |  | 		assert.strictEqual(params.bar, 'test'); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:36:18 -05:00
										 |  |  | 		assert.deepStrictEqual(params.herp, [2, 3]); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get a single param', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert.equal(utils.param('somekey'), undefined); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Webpack5 (#10311)
* feat: webpack 5 part 1
* fix: gruntfile fixes
* fix: fix taskbar warning
add app.importScript
copy public/src/modules to build folder
* refactor: remove commented old code
* feat: reenable admin
* fix: acp settings pages, fix sortable on manage categories
embedded require in html not allowed
* fix: bundle serialize/deserizeli so plugins dont break
* test: fixe util tests
* test: fix require path
* test: more test fixes
* test: require correct utils module
* test: require correct utils
* test: log stack
* test: fix db require blowing up tests
* test: move and disable bundle test
* refactor: add aliases
* test: disable testing route
* fix: move webpack modules necessary for build, into `dependencies`
* test: fix one more test
remove 500-embed.tpl
* fix: restore use of assets/nodebb.min.js, at least for now
* fix: remove unnecessary line break
* fix: point to proper ACP bundle
* test: maybe fix build test
* test: composer
* refactor: dont need dist
* refactor: more cleanup
use everything from build/public folder
* get rid of conditional import in app.js
* fix: ace
* refactor: cropper alias
* test: lint and test fixes
* lint: fix
* refactor: rename function to app.require
* refactor: go back to using app.require
* chore: use github branch
* chore: use webpack branch
* feat: webpack webinstaller
* feat: add chunkFile name with contenthash
* refactor: move hooks to top
* refactor: get rid of template500Function
* fix(deps): use webpack5 branch of 2factor plugin
* chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket:
* refactor: disable cache on templates
loadTemplate is called once by benchpress and the result is cache internally
* refactor: add server side helpers.js
* feat: deprecate /plugins shorthand route, closes #10343
* refactor: use build/public for webpack
* test: fix filename
* fix: more specific selector
* lint: ignore
* refactor: fix comments
* test: add debug for random failing test
* refactor: cleanup
remove test page, remove dupe functions in utils.common
* lint: use relative path  for now
* chore: bump prerelease version
* feat: add translateKeys
* fix: optional params
* fix: get rid of extra timeago files
* refactor: cleanup, require timeago locale earlier
remove translator.prepareDOM, it is in header.tpl html tag
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378)
* refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels
- Existing hooks are preserved (to be deprecated at a later date, possibly)
- New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks
* docs: fix typo in comment
* test: spec changes
* feat: allow app.require('bootbox'/'benchpressjs')
* refactor: require server side utils
* test: jquery ready
* change istaller to use build/public
* test: use document.addEventListener
* refactor: closes #10301
* refactor: generateTopicClass
* fix: column counts for other privileges
* fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking]
* fix: typo in hook name
* refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags)
* fix: crash if `delay` not passed in (as it cannot be destructured)
* refactor: replace substr
* feat: set --panel-offset style in html element based on stored value in localStorage
* refactor: addDropupHandler() logic to be less naive
- Take into account height of the menu
- Don't apply dropUp logic if there's nothing in the dropdown
- Remove 'hidden' class (added by default in Persona for post tools) when menu items are added
closes #10423
* refactor: simplify utils.params [breaking]
Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do.
* feat: add support for returning full URLSearchParams for utils.params
* fix: utils.params() fallback handling
* fix: default empty obj for params()
* fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end
* fix: utils.params() not allowing relative paths to be passed in
* refactor(DRY): new assertPasswordValidity utils method
* fix: incorrect error message returned on insufficient privilege on flag edit
* fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate
- added failing tests and patched up middleware.assert.flags to fix
* refactor: flag api v3 tests to create new post and flags on every round
* fix: missing error:no-flag language key
* refactor: flags.canView to check flag existence, simplify middleware.assert.flag
* feat: flag deletion API endpoint, #10426
* feat: UI for flag deletion, closes #10426
* chore: update plugin versions
* chore: up emoji
* chore: update markdown
* chore: up emoji-android
* fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check
Co-authored-by: Julian Lam <julian@nodebb.org>
											
										 
											2022-04-29 21:39:33 -04:00
										 |  |  | 	it('should get the full URLSearchParams object', async () => { | 
					
						
							|  |  |  | 		const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3', full: true }); | 
					
						
							|  |  |  | 		assert(params instanceof URLSearchParams); | 
					
						
							|  |  |  | 		assert.strictEqual(params.get('foo'), '1'); | 
					
						
							|  |  |  | 		assert.strictEqual(params.get('bar'), 'test'); | 
					
						
							|  |  |  | 		assert.strictEqual(params.get('herp[]'), '2'); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('toType', () => { | 
					
						
							|  |  |  | 		it('should return param as is if not string', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(123, utils.toType(123)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should convert return string numbers as numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(123, utils.toType('123')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should convert string "false" to boolean false', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.strictEqual(false, utils.toType('false')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should convert string "true" to boolean true', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.strictEqual(true, utils.toType('true')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should parse json', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const data = utils.toType('{"a":"1"}'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(data.a, '1'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return string as is if its not json,true,false or number', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const regularStr = 'this is a regular string'; | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(regularStr, utils.toType(regularStr)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('utils.props', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const data = {}; | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should set nested data', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(10, utils.props(data, 'a.b.c.d', 10)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return nested object', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const obj = utils.props(data, 'a.b.c'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(obj.d, 10); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should returned undefined without throwing', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(utils.props(data, 'a.b.c.foo.bar'), undefined); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return undefined if second param is null', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(utils.props(undefined, null), undefined); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('isInternalURI', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const target = { host: '', protocol: 'https' }; | 
					
						
							|  |  |  | 		const reference = { host: '', protocol: 'https' }; | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return true if they match', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert(utils.isInternalURI(target, reference, '')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return true if they match', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			target.host = 'nodebb.org'; | 
					
						
							|  |  |  | 			reference.host = 'nodebb.org'; | 
					
						
							|  |  |  | 			assert(utils.isInternalURI(target, reference, '')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should handle relative path', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			target.pathname = '/forum'; | 
					
						
							|  |  |  | 			assert(utils.isInternalURI(target, reference, '/forum')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return false if they do not match', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			target.pathname = ''; | 
					
						
							|  |  |  | 			reference.host = 'designcreateplay.com'; | 
					
						
							|  |  |  | 			assert(!utils.isInternalURI(target, reference)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('escape html', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const escaped = utils.escapeHTML('&<>'); | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		assert.equal(escaped, '&<>'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should escape regex chars', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const escaped = utils.escapeRegexChars('some text {}'); | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		assert.equal(escaped, 'some\\ text\\ \\{\\}'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get hours array', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const currentHour = new Date().getHours(); | 
					
						
							|  |  |  | 		const hours = utils.getHoursArray(); | 
					
						
							|  |  |  | 		let index = hours.length - 1; | 
					
						
							|  |  |  | 		for (let i = currentHour, ii = currentHour - 24; i > ii; i -= 1) { | 
					
						
							|  |  |  | 			const hour = i < 0 ? 24 + i : i; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(hours[index], `${hour}:00`); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 			index -= 1; | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get days array', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const currentDay = new Date(Date.now()).getTime(); | 
					
						
							|  |  |  | 		const days = utils.getDaysArray(); | 
					
						
							|  |  |  | 		const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | 
					
						
							|  |  |  | 		let index = 0; | 
					
						
							|  |  |  | 		for (let x = 29; x >= 0; x -= 1) { | 
					
						
							|  |  |  | 			const tmpDate = new Date(currentDay - (1000 * 60 * 60 * 24 * x)); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(`${months[tmpDate.getMonth()]} ${tmpDate.getDate()}`, days[index]); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 			index += 1; | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-09 14:35:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('`utils.rtrim` should remove trailing space', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-09 14:35:11 -06:00
										 |  |  | 		assert.strictEqual(utils.rtrim('  thing   '), '  thing'); | 
					
						
							|  |  |  | 		assert.strictEqual(utils.rtrim('\tthing\t\t'), '\tthing'); | 
					
						
							|  |  |  | 		assert.strictEqual(utils.rtrim('\t thing \t'), '\t thing'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should profile function', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const st = process.hrtime(); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			process.profile('it took', st); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-07-09 22:00:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return object with data', async () => { | 
					
						
							| 
									
										
										
										
											2019-07-09 22:00:46 -04:00
										 |  |  | 		const user = require('../src/user'); | 
					
						
							|  |  |  | 		const uid1 = await user.create({ username: 'promise1' }); | 
					
						
							|  |  |  | 		const uid2 = await user.create({ username: 'promise2' }); | 
					
						
							|  |  |  | 		const result = await utils.promiseParallel({ | 
					
						
							|  |  |  | 			user1: user.getUserData(uid1), | 
					
						
							|  |  |  | 			user2: user.getUserData(uid2), | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		assert(result.hasOwnProperty('user1') && result.hasOwnProperty('user2')); | 
					
						
							|  |  |  | 		assert.strictEqual(result.user1.uid, uid1); | 
					
						
							|  |  |  | 		assert.strictEqual(result.user2.uid, uid2); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2025-01-07 11:58:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe('debounce/throttle', () => { | 
					
						
							|  |  |  | 		it('should call function after x milliseconds once', (done) => { | 
					
						
							|  |  |  | 			let count = 0; | 
					
						
							|  |  |  | 			const now = Date.now(); | 
					
						
							|  |  |  | 			const fn = utils.debounce(() => { | 
					
						
							|  |  |  | 				count += 1; | 
					
						
							|  |  |  | 				assert.strictEqual(count, 1); | 
					
						
							|  |  |  | 				assert(Date.now() - now > 50); | 
					
						
							|  |  |  | 			}, 100); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			setTimeout(() => done(), 200); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should call function first if immediate=true', (done) => { | 
					
						
							|  |  |  | 			let count = 0; | 
					
						
							|  |  |  | 			const now = Date.now(); | 
					
						
							|  |  |  | 			const fn = utils.debounce(() => { | 
					
						
							|  |  |  | 				count += 1; | 
					
						
							|  |  |  | 				assert.strictEqual(count, 1); | 
					
						
							|  |  |  | 				assert(Date.now() - now < 50); | 
					
						
							|  |  |  | 			}, 100, true); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			setTimeout(() => done(), 200); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should call function after x milliseconds once', (done) => { | 
					
						
							|  |  |  | 			let count = 0; | 
					
						
							|  |  |  | 			const now = Date.now(); | 
					
						
							|  |  |  | 			const fn = utils.throttle(() => { | 
					
						
							|  |  |  | 				count += 1; | 
					
						
							|  |  |  | 				assert.strictEqual(count, 1); | 
					
						
							|  |  |  | 				assert(Date.now() - now > 50); | 
					
						
							|  |  |  | 			}, 100); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			setTimeout(() => done(), 200); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should call function twice if immediate=true', (done) => { | 
					
						
							|  |  |  | 			let count = 0; | 
					
						
							|  |  |  | 			const fn = utils.throttle(() => { | 
					
						
							|  |  |  | 				count += 1; | 
					
						
							|  |  |  | 			}, 100, true); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			fn(); | 
					
						
							|  |  |  | 			setTimeout(() => { | 
					
						
							|  |  |  | 				assert.strictEqual(count, 2); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}, 200); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2025-01-07 13:29:32 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe('Translator', () => { | 
					
						
							|  |  |  | 		const shim = require('../src/translator'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const { Translator } = shim; | 
					
						
							|  |  |  | 		it('should translate in place', async () => { | 
					
						
							|  |  |  | 			const translator = Translator.create('en-GB'); | 
					
						
							|  |  |  | 			const el = $(`<div><span id="search" title="[[global:search]]"></span><span id="text">[[global:home]]</span></div>`); | 
					
						
							|  |  |  | 			await translator.translateInPlace(el.get(0)); | 
					
						
							|  |  |  | 			assert.strictEqual(el.find('#text').text(), 'Home'); | 
					
						
							|  |  |  | 			assert.strictEqual(el.find('#search').attr('title'), 'Search'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2025-01-07 13:55:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not error', (done) => { | 
					
						
							|  |  |  | 			shim.flush(); | 
					
						
							|  |  |  | 			shim.flushNamespace(); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2025-01-07 13:29:32 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | }); |