HEX
Server: LiteSpeed
System: Linux server342.web-hosting.com 4.18.0-553.124.4.lve.el8.x86_64 #1 SMP Fri May 15 13:02:13 UTC 2026 x86_64
User: ksonpoau (1099)
PHP: 8.2.31
Disabled: NONE
Upload Files
File: //home/ksonpoau/www/wp-content/plugins/extendify/src/Library/util/replace-pattern-images.js
const formatImageUrl = (image) =>
	image?.includes('?q=80&w=1470') ? image : `${image}?q=80&w=1470`;

const hashString = (s) => {
	let h = 0;
	for (let i = 0; i < s.length; i++) {
		h = (h * 31 + s.charCodeAt(i)) | 0;
	}
	return Math.abs(h);
};

const seededShuffle = (arr, seed) => {
	const result = [...arr];
	let h = hashString(String(seed));
	for (let i = result.length - 1; i > 0; i--) {
		h = (h * 1103515245 + 12345) | 0;
		const j = Math.abs(h) % (i + 1);
		[result[i], result[j]] = [result[j], result[i]];
	}
	return result;
};

export const replacePatternImages = (content, images, seed) => {
	if (!images?.length) return content;
	const matches =
		content.match(/https:\/\/images\.unsplash\.com\/[^\s"]+/g) || [];
	const uniqueUrls = [...new Set(matches)];
	const order = seededShuffle(images, seed);
	return uniqueUrls.reduce(
		(updated, url, i) =>
			updated.replaceAll(url, formatImageUrl(order[i] || url)),
		content,
	);
};