1.0.4 • Published 7 years ago

@yct/utils v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
7 years ago

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
```ts
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);
  }
});
1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.1

8 years ago

0.1.0

8 years ago