'use strict'; const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj || {}, key); const toInt = (value, fallback = null) => { const num = Number.parseInt(value, 10); return Number.isInteger(num) ? num : fallback; }; const isPositiveInt = value => Number.isInteger(Number(value)) && Number(value) > 0; const parsePagination = (query = {}) => { const page = Math.max(toInt(query.page, 1), 1); const pageSize = Math.min(Math.max(toInt(query.pageSize, 20), 1), 5000); return { page, pageSize, offset: (page - 1) * pageSize, limit: pageSize, }; }; const normalizeParentId = value => { if (value === null || value === undefined || value === '') return null; const parsed = toInt(value); if (!parsed || parsed < 1) throw '参数错误: parentId'; return parsed; }; const normalizeNullableInt = (value, label = 'value') => { if (value === null || value === undefined || value === '') return null; const parsed = toInt(value); if (!parsed || parsed < 1) throw `参数错误: ${label}`; return parsed; }; const normalizeInsertPosition = (rawPosition, maxPosition) => { const parsed = toInt(rawPosition, maxPosition); if (!parsed || parsed < 1) return maxPosition; if (parsed > maxPosition) return maxPosition; return parsed; }; const normalizeOrderedInput = (items = []) => { return (items || []) .map((item, index) => ({ item, sortPos: isPositiveInt(item?.position) ? Number(item.position) : index + 1, index, })) .sort((a, b) => a.sortPos - b.sortPos || a.index - b.index) .map(entry => entry.item); }; const ensureCreatedBy = (body = {}) => { const createdBy = String(body.createdBy || '').trim(); if (!createdBy) throw '缺少参数: createdBy'; return createdBy; }; const ensureNonEmptyString = (value, label = 'value') => { const normalized = String(value || '').trim(); if (!normalized) throw `missing param: ${label}`; return normalized; }; const ensureObjectPayload = (value, label = 'payload') => { if (!value || typeof value !== 'object' || Array.isArray(value)) { throw `invalid param: ${label}`; } return value; }; module.exports = { hasOwn, toInt, isPositiveInt, parsePagination, normalizeParentId, normalizeNullableInt, normalizeInsertPosition, normalizeOrderedInput, ensureCreatedBy, ensureNonEmptyString, ensureObjectPayload, };