UNPKG

@sap-ux/store

Version:

NPM module for storing persistent data

92 lines 2.82 kB
import { homedir } from 'node:os'; import { join } from 'node:path'; import pluralize from 'pluralize'; const plural = pluralize.plural; /** Pick the properties listed and return a new object with a shallow-copy */ export const pick = (target, ...props) => { return ((target && props?.length > 0 && props.reduce((o, k) => { o[k] = target[k]; return o; }, {})) || undefined); }; /** * Checks if any of the values in the object are not `undefined` or `null` * * @param obj - the object to check * @param props - the properties to check on the object * @returns - `true` if any value is not `undefined` or `null`, `false` otherwise */ export function hasAnyValue(obj, props) { if (obj == null || typeof obj !== 'object') { return false; } return props.some((prop) => { const value = obj[prop]; return value !== undefined && value !== null; }); } /** Given an `Error` or any other object thrown, returns an `Error` instance */ export function errorInstance(e) { if (e instanceof Error) { return e; } else { return new Error(String(e)); } } /** If input in an instance of `Error` return the message property, * otherwise convert the input to its string representation */ export function errorString(e) { return e instanceof Error ? e.message : String(e); } export var FioriToolsSettings; (function (FioriToolsSettings) { FioriToolsSettings["dir"] = ".fioritools"; })(FioriToolsSettings || (FioriToolsSettings = {})); export var SapTools; (function (SapTools) { SapTools["dir"] = ".saptools"; })(SapTools || (SapTools = {})); export const getFioriToolsDirectory = () => { return join(homedir(), FioriToolsSettings.dir); }; export const getSapToolsDirectory = () => { return join(homedir(), SapTools.dir); }; /** * Trims, lowercases and returns plural if a non-empty string * * @param s */ export function toPersistenceName(s) { const t = s?.trim().toLowerCase(); return t && plural(t); } export function getEntityFileName(entityName) { return toPersistenceName(entityName) + '.json'; } /** * Simple object matcher that supports nested objects * * @param obj - object to inspect * @param filter - properties and values used for filtering the object. * @returns - true if the `obj` has equivalent property values to `attrs` */ export function isMatch(obj, filter) { return Object.entries(filter).every(([key, val]) => { if (Array.isArray(val)) { return val.includes(obj[key]); } if (val && typeof val === 'object') { return isMatch(obj[key], val); } return obj[key] === val; }); } export * from './app-studio.js'; export * from './backend.js'; //# sourceMappingURL=index.js.map