Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cactuslab/api-helpers

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cactuslab/api-helpers - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

16

dist/helpers.d.ts

@@ -7,3 +7,17 @@ /**

*/
export declare function delay<R>(delay: number, func: () => Promise<R>): Promise<R>;
export declare function delay<R = null>(delay: number, func?: () => Promise<R>): Promise<R>;
/**
* Apply a patch to an object for the purposes of simulating patch requests.
* @param target the target object, which will be shallow cloned for the result
* @param patch the patch object
* @returns a new object containing the result of applying the patch to the target
*/
export declare function applyPatch<T extends Record<string, unknown>>(target: T, patch: Patch<T>): T;
export declare type Patch<T> = {
[K in keyof T]?: NonNullable<T[K]> | null;
};
/**
* Create a patch object representing the changes between an original and an updated object.
*/
export declare function createPatch<T extends Record<string, unknown>>(original: T, updated: T): Patch<T>;
//# sourceMappingURL=helpers.d.ts.map
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.delay = void 0;
exports.createPatch = exports.applyPatch = exports.delay = void 0;
/**

@@ -13,3 +24,8 @@ * Delay calling a promise for the purposes of simulating things taking time, such as API calls.

setTimeout(function () {
func().then(resolve).catch(reject);
if (func) {
func().then(resolve).catch(reject);
}
else {
resolve(null);
}
}, delay);

@@ -19,1 +35,54 @@ });

exports.delay = delay;
/**
* Apply a patch to an object for the purposes of simulating patch requests.
* @param target the target object, which will be shallow cloned for the result
* @param patch the patch object
* @returns a new object containing the result of applying the patch to the target
*/
function applyPatch(target, patch) {
var result = __assign({}, target);
for (var _i = 0, _a = Object.keys(patch); _i < _a.length; _i++) {
var key = _a[_i];
var value = patch[key];
if (value !== undefined) {
if (value === null) {
delete result[key];
}
else {
result[key] = value;
}
}
}
return result;
}
exports.applyPatch = applyPatch;
/**
* Create a patch object representing the changes between an original and an updated object.
*/
function createPatch(original, updated) {
var result = {};
var seenKeys = new Set();
for (var _i = 0, _a = Object.keys(original); _i < _a.length; _i++) {
var key = _a[_i];
seenKeys.add(key);
var originalValue = original[key];
var newValue = updated[key];
if (originalValue !== newValue) {
if (newValue === undefined) {
result[key] = null;
}
else {
result[key] = newValue;
}
}
}
for (var _b = 0, _c = Object.keys(updated); _b < _c.length; _b++) {
var key = _c[_b];
if (!seenKeys.has(key)) {
var newValue = updated[key];
result[key] = newValue;
}
}
return result;
}
exports.createPatch = createPatch;

2

package.json
{
"name": "@cactuslab/api-helpers",
"version": "0.0.1",
"version": "0.1.0",
"description": "Helpers for integrating with a backend API.",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -7,8 +7,68 @@ /**

*/
export function delay<R>(delay: number, func: () => Promise<R>): Promise<R> {
export function delay<R = null>(delay: number, func?: () => Promise<R>): Promise<R> {
return new Promise((resolve, reject) => {
setTimeout(() => {
func().then(resolve).catch(reject)
if (func) {
func().then(resolve).catch(reject)
} else {
resolve(null as unknown as R)
}
}, delay)
})
}
/**
* Apply a patch to an object for the purposes of simulating patch requests.
* @param target the target object, which will be shallow cloned for the result
* @param patch the patch object
* @returns a new object containing the result of applying the patch to the target
*/
export function applyPatch<T extends Record<string, unknown>>(target: T, patch: Patch<T>): T {
const result: T = {
...target,
}
for (const key of Object.keys(patch)) {
const value = patch[key]
if (value !== undefined) {
if (value === null) {
delete result[key]
} else {
result[key as keyof T] = value
}
}
}
return result
}
export type Patch<T> = {
[K in keyof T]?: NonNullable<T[K]> | null
}
/**
* Create a patch object representing the changes between an original and an updated object.
*/
export function createPatch<T extends Record<string, unknown>>(original: T, updated: T): Patch<T> {
const result: Patch<T> = {}
const seenKeys = new Set()
for (const key of Object.keys(original)) {
seenKeys.add(key)
const originalValue = original[key]
const newValue = updated[key]
if (originalValue !== newValue) {
if (newValue === undefined) {
result[key as keyof T] = null
} else {
result[key as keyof T] = newValue as any
}
}
}
for (const key of Object.keys(updated)) {
if (!seenKeys.has(key)) {
const newValue = updated[key]
result[key as keyof T] = newValue as any
}
}
return result
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc