First upload version 0.0.1

This commit is contained in:
Neyra
2026-02-05 15:27:49 +08:00
commit 8e9b7201ed
4182 changed files with 593136 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
import { MissingPageInfo } from "./errors.js";
const isObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
function findPaginatedResourcePath(responseData) {
const paginatedResourcePath = deepFindPathToProperty(
responseData,
"pageInfo"
);
if (paginatedResourcePath.length === 0) {
throw new MissingPageInfo(responseData);
}
return paginatedResourcePath;
}
const deepFindPathToProperty = (object, searchProp, path = []) => {
for (const key of Object.keys(object)) {
const currentPath = [...path, key];
const currentValue = object[key];
if (isObject(currentValue)) {
if (currentValue.hasOwnProperty(searchProp)) {
return currentPath;
}
const result = deepFindPathToProperty(
currentValue,
searchProp,
currentPath
);
if (result.length > 0) {
return result;
}
}
}
return [];
};
const get = (object, path) => {
return path.reduce((current, nextProperty) => current[nextProperty], object);
};
const set = (object, path, mutator) => {
const lastProperty = path[path.length - 1];
const parentPath = [...path].slice(0, -1);
const parent = get(object, parentPath);
if (typeof mutator === "function") {
parent[lastProperty] = mutator(parent[lastProperty]);
} else {
parent[lastProperty] = mutator;
}
};
export {
findPaginatedResourcePath,
get,
set
};