Skip to content

yc-typescript/utils

Repository files navigation

Build Status Coverage Status MIT license

Installation

npm i -S @yct/utils

Functions

fetch

browser only

function fetch(
  method: string,
  url: string,
  params: any = {},
  headers: any = {},
  json: boolean = false,
): Promise<any>

Usage

import { fetch } from "@yct/utils";

const res = await fetch('GET', 'http://localhost');

isPromise

import { isPromise } from '@yct/utils';

test('Should be true', () => {
  expect(isPromise(Promise.resolve(0))).toBe(true);
});

test('Should be false', () => {
  expect(isPromise({})).toBe(false);
  expect(isPromise(1)).toBe(false);
  expect(isPromise('hello')).toBe(false);
  expect(isPromise(true)).toBe(false);
});

resolveObject

import { resolveObject } from '@yct/utils';

test('Should resolve all', async () => {
  try {
    const obj = {
      a: Promise.resolve('a'),
      b: {
        b1: Promise.resolve('b1'),
        b2: 'b2',
        b3: null,
      },
      c: [
        {
          c1: Promise.resolve('c1'),
        },
      ],
    };
    await resolveObject(obj);
    expect(obj).toMatchObject({
      a: 'a',
      b: {
        b1: 'b1',
        b2: 'b2',
        b3: null,
      },
      c: [
        {
          c1: 'c1',
        },
      ],
    });
  } catch(e) {
    console.error(e);
  }
});

loadScript

import { loadScript } from '@yct/utils';

declare const window: any;

test('Should load jssdk', async () => {
  try {
    await loadScript('http://res.wx.qq.com/open/js/jweixin-1.2.0.js');
    expect(window.wx).toBeTruthy();
  } catch (e) {
    console.error(e);
  }
});

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published