@premieroctet/next-admin
Version:
Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje
67 lines (66 loc) • 3.13 kB
JavaScript
import external_lodash_sortby_default from "lodash.sortby";
import external_react_default from "react";
const capitalize = (str)=>{
let capitalizedStr = str.charAt(0).toLocaleUpperCase() + str.slice(1);
return capitalizedStr;
};
const uncapitalize = (str)=>{
let uncapitalizedStr = str.charAt(0).toLocaleLowerCase() + str.slice(1);
return uncapitalizedStr;
};
const isNativeFunction = (fn)=>/\{\s*\[native code\]\s*\}/.test(fn.toString());
const isScalar = (value)=>"string" == typeof value || "boolean" == typeof value || "number" == typeof value;
const pipe = (...fns)=>(x)=>fns.reduce(async (v, f)=>await f(await v), Promise.resolve(x));
const extractSerializable = (obj, isAppDir)=>{
if (Array.isArray(obj)) return obj.map((item)=>extractSerializable(item, isAppDir));
if (obj instanceof Date) return obj.toISOString();
if (null === obj) return obj;
if ("object" == typeof obj) {
if (isAppDir && external_react_default.isValidElement(obj)) return null;
let newObj = {};
for(const key in obj)if (obj.hasOwnProperty(key)) newObj = {
...newObj,
[key]: extractSerializable(obj[key], isAppDir)
};
return newObj;
}
if (isScalar(obj)) return obj;
else return null;
};
const slugify = (str)=>str.toLowerCase();
const formatLabel = (label)=>{
let spacedLabel = label.replace(/_/g, " ").replace(/([A-Z])/g, " $1").trim();
return capitalize(spacedLabel.toLowerCase());
};
const isUploadFile = (obj)=>"object" == typeof obj && "buffer" in obj && "infos" in obj && obj.buffer.length > 0;
const getDisplayedValue = (element)=>{
if ("string" == typeof element) return element;
if (external_react_default.isValidElement(element)) return Array.prototype.map.call(element.props.children, (child)=>getDisplayedValue(child)).join("");
return "";
};
const getDeletedFilesFieldName = (field)=>`${field}__nextadmin_deleted`;
const isFileUploadFormat = (format)=>[
"data-url",
"file"
].includes(format);
const reorderData = (data, fromId, toId, orderField, idField)=>{
const result = Array.from(data);
const from = result.find((item)=>item[idField].value === fromId);
const to = result.find((item)=>item[idField].value === toId);
if (!from || !to) return result;
const fromIndex = result.indexOf(from);
const toIndex = result.indexOf(to);
result[fromIndex][orderField].value = to[orderField].value;
if (fromIndex < toIndex) for(let i = fromIndex + 1; i <= toIndex; i++){
const current = result[i][orderField].value;
result[i][orderField].value = current - 1;
}
else for(let i = toIndex; i < fromIndex; i++){
const current = result[i][orderField].value;
result[i][orderField].value = current + 1;
}
return external_lodash_sortby_default(result, function(item) {
return item[orderField].value;
});
};
export { capitalize, extractSerializable, formatLabel, getDeletedFilesFieldName, getDisplayedValue, isFileUploadFormat, isNativeFunction, isScalar, isUploadFile, pipe, reorderData, slugify, uncapitalize };