Skip to content

Commit

Permalink
feat: add randomString, jsonp
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 30, 2018
1 parent c792e82 commit 320d0aa
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ export { default as isPromise } from './isPromise'
export { default as isRegExp } from './isRegExp'
export { default as isString } from './isString'
export { default as isUndefined } from './isUndefined'
export { default as jsonp } from './jsonp'
export { default as mapValues } from './mapValues'
export { default as noop } from './noop'
export { default as omit } from './omit'
export { default as parseCssValue } from './parseCssValue'
export { default as pick } from './pick'
export { default as preventEventDefault } from './preventEventDefault'
export { default as randomString } from './randomString'
export { default as reduce } from './reduce'
export { default as repeat } from './repeat'
export { default as shuffle } from './shuffle'
Expand Down
48 changes: 48 additions & 0 deletions src/jsonp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import inBrowser from './inBrowser'
import isObject from './isObject'
import noop from './noop'
import randomString from './randomString'
import reduce from './reduce'

const objectToQueryString = (obj: object): string => {
return reduce(
obj,
(str, value, key) => str += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
''
)
}

export default function jsonp(url: string, data?: string | object, callbackParameter = 'callback'): Promise<any> {
return new Promise(
inBrowser()
? (resolve, reject) => {
let script: HTMLScriptElement

const callbackFunctionName = `jsonp${randomString()}`
const queryString = `${
isObject(data) ? objectToQueryString(data) : (data != null ? String(data) : '')
}&${
encodeURIComponent(callbackParameter)
}=${
encodeURIComponent(callbackFunctionName)
}`
const requestUrl = url + (url.indexOf('?') > -1 ? '&' : '?') + queryString;

(window as any)[callbackFunctionName] = (response: any) => {
resolve(response)
try {
delete (window as any)[callbackFunctionName]
document.body.removeChild(script)
} catch (err) {}
}

script = document.createElement('script')
script.src = requestUrl
script.async = true
script.onerror = reject

document.body.appendChild(script)
}
: noop
)
}
8 changes: 8 additions & 0 deletions src/randomString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 返回一个随机的字符串。
*
* @returns 随机的字符串
*/
export default function randomString(): string {
return Math.random().toString(36).substr(2)
}
24 changes: 24 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,27 @@ describe('storage', () => {
expect(vtils.storage.get('hello')).toBeNull()
})
})

describe('randomString', () => {
test('ok', () => {
const r: string[] = []
for (let i = 0; i < 10000; i++) {
const str = vtils.randomString()
expect(r.indexOf(str) === -1).toBeTruthy()
r.push(str)
}
})
})

// describe('jsonp', () => {
// test('ok', done => {
// vtils.jsonp('https://jsonplaceholder.typicode.com/todos/1')
// .then(obj => {
// expect(obj).toContainEqual({
// userId: 1,
// id: 1
// })
// done()
// })
// })
// })

0 comments on commit 320d0aa

Please sign in to comment.