cody-utils 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/.history/package_20200212101131.json +19 -0
  2. package/.history/package_20250527165758.json +19 -0
  3. package/.history/package_20250527165800.json +19 -0
  4. package/.history/package_20250527165806.json +19 -0
  5. package/.history/package_20250527165810.json +19 -0
  6. package/.history/package_20250527165815.json +19 -0
  7. package/.history/package_20250527165817.json +19 -0
  8. package/.history/package_20250527165826.json +19 -0
  9. package/.history/package_20250527165829.json +19 -0
  10. package/.history/package_20250527165835.json +19 -0
  11. package/.history/package_20250527170050.json +19 -0
  12. package/.history/package_20250527170051.json +19 -0
  13. package/.history/webpack.config_20200212101131.js +23 -0
  14. package/.history/webpack.config_20250527165622.js +23 -0
  15. package/.history/webpack.config_20250527165631.js +23 -0
  16. package/.history/webpack.config_20250527165643.js +23 -0
  17. package/.history/webpack.config_20250527165647.js +23 -0
  18. package/README.md +124 -0
  19. package/dist/cody-utils.min.js +435 -0
  20. package/package.json +19 -0
  21. package/src/array/chunk.js +30 -0
  22. package/src/array/compact.js +6 -0
  23. package/src/array/concat.js +16 -0
  24. package/src/array/declares.js +92 -0
  25. package/src/array/difference.js +12 -0
  26. package/src/array/drop.js +28 -0
  27. package/src/array/flatten.js +28 -0
  28. package/src/array/merge.js +18 -0
  29. package/src/array/pull.js +33 -0
  30. package/src/array/slice.js +33 -0
  31. package/src/array/unique.js +40 -0
  32. package/src/axios/index.js +97 -0
  33. package/src/event-bus/index.js +46 -0
  34. package/src/function/apply.js +17 -0
  35. package/src/function/bind.js +18 -0
  36. package/src/function/call.js +17 -0
  37. package/src/function/debounce.js +22 -0
  38. package/src/function/throttle.js +15 -0
  39. package/src/index.js +30 -0
  40. package/src/object/clone.js +34 -0
  41. package/src/object/deepClone.js +105 -0
  42. package/src/object/merge.js +14 -0
  43. package/src/object/myInstanceOf.js +22 -0
  44. package/src/object/newInstance.js +20 -0
  45. package/src/promise/index.js +226 -0
  46. package/src/pub-sub/index.js +95 -0
  47. package/src/string/index.js +28 -0
  48. package/test/01_call_apply_bind.html +47 -0
  49. package/test/02_throttle_debounce.html +54 -0
  50. package/test/02_/345/207/275/346/225/260/351/230/262/346/212/226/344/270/216/350/212/202/346/265/201.jpg +0 -0
  51. package/test/03_array_declare.html +51 -0
  52. package/test/04_array_unique.html +32 -0
  53. package/test/05_array_concat_slice.html +28 -0
  54. package/test/06_array_flatten.html +23 -0
  55. package/test/07_array_compact_chunk.html +25 -0
  56. package/test/08_array_difference_merge.html +22 -0
  57. package/test/09_array_pull_pullAll.html +27 -0
  58. package/test/10_array_drop_dropRight.html +32 -0
  59. package/test/11_new_instanceof.html +47 -0
  60. package/test/12_object_merge.html +34 -0
  61. package/test/13_deepClone.html +42 -0
  62. package/test/13_shallowClone.html +27 -0
  63. package/test/13_/346/265/213/350/257/225/351/201/215/345/216/206/345/257/271/350/261/241/346/225/210/347/216/207.html +38 -0
  64. package/test/13_/346/265/213/350/257/225/351/201/215/345/216/206/346/225/260/347/273/204/346/225/210/347/216/207.html +60 -0
  65. package/test/14_string.html +21 -0
  66. package/test/15_/346/211/213/345/206/231/347/273/247/346/211/277.html +53 -0
  67. package/test/16_/346/211/213/345/206/231/344/272/213/344/273/266/346/200/273/347/272/277.html +32 -0
  68. package/test/17_/346/211/213/345/206/231/346/266/210/346/201/257/350/256/242/351/230/205/344/270/216/345/217/221/345/270/203.html +48 -0
  69. package/test/18_/346/211/213/345/206/231Promise.html +49 -0
  70. package/test/19_/346/211/213/345/206/231ajax/350/257/267/346/261/202/345/207/275/346/225/260.html +102 -0
  71. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/01_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/346/225/260/347/273/204//346/225/260/347/273/204/345/270/270/350/247/201/346/223/215/344/275/2341.html +48 -0
  72. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/01_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/346/225/260/347/273/204//346/225/260/347/273/204/345/270/270/350/247/201/346/223/215/344/275/2342.html +74 -0
  73. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/01_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/346/225/260/347/273/204//346/225/260/347/273/204/345/270/270/350/247/201/346/223/215/344/275/2343.html +53 -0
  74. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/01_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/346/225/260/347/273/204//346/225/260/347/273/204/345/270/270/350/247/201/346/223/215/344/275/2344.html +54 -0
  75. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/02_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/346/240/210//346/240/210_/345/272/224/347/224/250_/345/215/201/350/277/233/345/210/266/350/275/254/344/272/214/350/277/233/345/210/266.html +41 -0
  76. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/02_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/346/240/210//346/240/210_/346/265/213/350/257/225.html +29 -0
  77. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/03_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/351/230/237/345/210/227//344/274/230/345/205/210/347/272/247/351/230/237/345/210/227_/346/265/213/350/257/225.html +29 -0
  78. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/03_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/351/230/237/345/210/227//351/230/237/345/210/227_/345/272/224/347/224/250_/345/207/273/351/274/223/344/274/240/350/212/261.html +38 -0
  79. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/03_/345/237/272/346/234/254/346/225/260/346/215/256/347/273/223/346/236/204_/351/230/237/345/210/227//351/230/237/345/210/227_/346/265/213/350/257/225.html +33 -0
  80. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/04_/346/225/260/346/215/256/347/273/223/346/236/204_/351/223/276/350/241/250//345/217/214/345/220/221/351/223/276/350/241/250_/346/265/213/350/257/225.html +55 -0
  81. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/04_/346/225/260/346/215/256/347/273/223/346/236/204_/351/223/276/350/241/250//351/223/276/350/241/250_/346/265/213/350/257/225.html +52 -0
  82. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/05_/346/225/260/346/215/256/347/273/223/346/236/204_/346/240/221//344/272/214/345/217/211/346/220/234/347/264/242/346/240/221_/346/265/213/350/257/225.html +48 -0
  83. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/06_/346/225/260/346/215/256/347/273/223/346/236/204_/351/233/206/345/220/210//351/233/206/345/220/210_/346/265/213/350/257/225.html +50 -0
  84. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/07_/346/216/222/345/272/217/347/256/227/346/263/225/3/347/247/215/345/237/272/346/234/254/346/216/222/345/272/217_/346/265/213/350/257/225.html +19 -0
  85. package/test/20_/346/225/260/346/215/256/347/273/223/346/236/204/344/270/216/347/256/227/346/263/225/07_/346/216/222/345/272/217/347/256/227/346/263/225//351/253/230/347/272/247/346/216/222/345/272/217_/346/265/213/350/257/225.html +19 -0
  86. package/test/README.md +207 -0
  87. package/test/db.json +54 -0
  88. package/webpack.config.js +23 -0
  89. package//350/207/252/345/256/232/344/271/211npm/345/267/245/345/205/267/345/272/223.md +91 -0
@@ -0,0 +1,92 @@
1
+ /*
2
+ 实现数组声明式处理系列工具函数
3
+ */
4
+
5
+ /*
6
+ 实现map()
7
+ */
8
+ export function map (array, callback) {
9
+ const arr = []
10
+ for (let index = 0; index < array.length; index++) {
11
+ arr.push(callback(array[index], index))
12
+ }
13
+ return arr
14
+ }
15
+
16
+ /*
17
+ 实现reduce()
18
+ */
19
+ export function reduce (array, callback, initValue) {
20
+ let result = initValue
21
+ for (let index = 0; index < array.length; index++) {
22
+ // 调用回调函数将返回的结果赋值给result
23
+ result = callback(result, array[index], index)
24
+ }
25
+ return result
26
+ }
27
+
28
+ /*
29
+ 实现filter()
30
+ */
31
+ export function filter(array, callback) {
32
+
33
+ const arr = []
34
+ for (let index = 0; index < array.length; index++) {
35
+ if (callback(array[index], index)) {
36
+ arr.push(array[index])
37
+ }
38
+ }
39
+ return arr
40
+ }
41
+
42
+ /*
43
+ 实现find()
44
+ */
45
+ export function find (array, callback) {
46
+ for (let index = 0; index < array.length; index++) {
47
+ if (callback(array[index], index)) {
48
+ return array[index]
49
+ }
50
+ }
51
+ return undefined
52
+ }
53
+
54
+ /*
55
+ 实现findIndex()
56
+ */
57
+ export function findIndex (array, callback) {
58
+ for (let index = 0; index < array.length; index++) {
59
+ if (callback(array[index], index)) {
60
+ return index
61
+ }
62
+ }
63
+ return -1
64
+ }
65
+
66
+ /*
67
+ 实现every()
68
+ */
69
+ export function every (array, callback) {
70
+ for (let index = 0; index < array.length; index++) {
71
+ if (!callback(array[index], index)) { // 只有一个结果为false, 直接返回false
72
+ return false
73
+ }
74
+ }
75
+ return true
76
+ }
77
+
78
+ /*
79
+ 实现some()
80
+ */
81
+ export function some (array, callback) {
82
+ for (let index = 0; index < array.length; index++) {
83
+ if (callback(array[index], index)) { // 只有一个结果为true, 直接返回true
84
+ return true
85
+ }
86
+ }
87
+ return false
88
+ }
89
+
90
+ export function test() {
91
+ console.log('test()222')
92
+ }
@@ -0,0 +1,12 @@
1
+ /*
2
+ difference(arr1, arr2): 得到arr1中所有不在arr2中的元素组成的数组(不改变原数组)
3
+ 如: [1,3,5,7].difference([5, 8]) ==> [1, 3, 7]
4
+ */
5
+ export function difference (arr1, arr2) {
6
+ if (arr1.length===0) {
7
+ return []
8
+ } else if (arr2.length===0) {
9
+ return arr1.slice()
10
+ }
11
+ return arr1.filter(item => arr2.indexOf(item)===-1)
12
+ }
@@ -0,0 +1,28 @@
1
+ /*
2
+ 1. drop(array, count):
3
+ 得到数组过滤掉左边count个后剩余元素组成的数组
4
+ 说明: 不改变当前数组, count默认是1
5
+ 如: drop([1,3,5,7], 2) ===> [5, 7]
6
+ 2. dropRight(array, count):
7
+ 得到数组过滤掉右边count个后剩余元素组成的数组
8
+ 说明: 不改变数组, count默认是1
9
+ 如: dropRight([1,3,5,7], 2) ===> [1, 3]
10
+ */
11
+
12
+ export function drop (array, count) {
13
+ if (array.length === 0 || count >= array.length) {
14
+ return []
15
+ }
16
+ count = count || 1
17
+
18
+ return array.filter((item, index) => index>=count)
19
+ }
20
+
21
+ export function dropRight (array, count) {
22
+ if (array.length === 0 || count >= array.length) {
23
+ return []
24
+ }
25
+ count = count || 1
26
+
27
+ return array.filter((item, index) => index < array.length-count)
28
+ }
@@ -0,0 +1,28 @@
1
+ /*
2
+ 数组扁平化: 取出嵌套数组(多维)中的所有元素放到一个新数组(一维)中
3
+ 如: [1, [3, [2, 4]]] ==> [1, 3, 2, 4]
4
+ */
5
+
6
+ /*
7
+ 方法一: 递归 + reduce() + concat()
8
+ */
9
+ export function flatten1 (array) {
10
+ return array.reduce((pre, item) => {
11
+ if (Array.isArray(item)) {
12
+ return pre.concat(flatten1(item))
13
+ } else {
14
+ return pre.concat(item)
15
+ }
16
+ }, [])
17
+ }
18
+
19
+ /*
20
+ 方法二: ... + some() + concat()
21
+ */
22
+ export function flatten2 (array) {
23
+ let arr = [].concat(...array)
24
+ while (arr.some(item => Array.isArray(item))) {
25
+ arr = [].concat(...arr)
26
+ }
27
+ return arr
28
+ }
@@ -0,0 +1,18 @@
1
+ /*
2
+ merge(arr1, arr2): 将arr2与arr1的元素进行合并组成一个新的数组(不改变原数组)
3
+ 如: merge([1,3,5,7,5], [1, 5, 8]) ==> [1, 3, 5, 7, 5, 8]
4
+ */
5
+
6
+ export function mergeArray (arr1, arr2) {
7
+ var result = arr1.slice()
8
+ if (!arr2 || arr2.length===0) {
9
+ return result
10
+ }
11
+ arr2.forEach(item => {
12
+ if (result.indexOf(item)===-1) {
13
+ result.push(item)
14
+ }
15
+ })
16
+
17
+ return result
18
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ 1. pull(array, ...values):
3
+ 删除数组中与value相同的元素, 返回所有删除元素的数组
4
+ 说明: 数组发生了改变
5
+ 如: pull([1,3,5,3,7], 2, 7, 3, 7) ===> 数组变为[1, 5], 返回值为[3,3,7]
6
+ 2. pullAll(array, values):
7
+ 功能与pull一致, 只是参数变为数组
8
+ 如: pullAll([1,3,5,3,7], [2, 7, 3, 7]) ===> 数组变为[1, 5], 返回值为[3,3,7]
9
+ */
10
+
11
+ export function pull (array, ...values) {
12
+ if (array.length===0 || values.length===0) {
13
+ return []
14
+ }
15
+ values = Array.from(new Set(values))
16
+ var result = []
17
+ for (let index = 0; index < array.length; index++) {
18
+ const item = array[index];
19
+ if (values.indexOf(item)!==-1) {
20
+ array.splice(index, 1)
21
+ result.push(item)
22
+ index--
23
+ }
24
+ }
25
+ return result
26
+ }
27
+
28
+ export function pullAll (array, values) {
29
+ if (!values || !Array.isArray(values)) {
30
+ return []
31
+ }
32
+ return pull(array, ...values)
33
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ 语法: var new_array = slice(oldArr, [begin[, end]])
3
+ 功能: 返回一个由 begin 和 end 决定的原数组的浅拷贝, 原始数组不会被改变
4
+ */
5
+ export function slice (array, begin, end) {
6
+ // 如果当前数组是[], 直接返回[]
7
+ if (array.length === 0) {
8
+ return []
9
+ }
10
+
11
+ // 如果begin超过最大下标, 直接返回[]
12
+ begin = begin || 0
13
+ if (begin >= array.length) {
14
+ return []
15
+ }
16
+
17
+ // 如果end不大于begin, 直接返回[]
18
+ end = end || array.length
19
+ if (end > array.length) {
20
+ end = array.length
21
+ }
22
+ if (end <= begin) {
23
+ return []
24
+ }
25
+
26
+ // 取出下标在[begin, end)区间的元素, 并保存到最终的数组中
27
+ const arr = []
28
+ for (let index = begin; index < end; index++) {
29
+ arr.push(array[index])
30
+ }
31
+
32
+ return arr
33
+ }
@@ -0,0 +1,40 @@
1
+ /*
2
+ 方法1: 利用forEach()和indexOf()
3
+ 说明: 本质是双重遍历, 效率差些
4
+ */
5
+ export function unique1 (array) {
6
+ const arr = []
7
+ array.forEach(item => {
8
+ if (arr.indexOf(item)===-1) {
9
+ arr.push(item)
10
+ }
11
+ })
12
+ return arr
13
+ }
14
+
15
+ /*
16
+ 方法2: 利用forEach() + 对象容器
17
+ 说明: 只需一重遍历, 效率高些
18
+ */
19
+ export function unique2 (array) {
20
+ const arr = []
21
+ const obj = {}
22
+ array.forEach(item => {
23
+ if (!obj.hasOwnProperty(item)) {
24
+ obj[item] = true
25
+ arr.push(item)
26
+ }
27
+ })
28
+ return arr
29
+ }
30
+
31
+ /*
32
+ 方法3: 利用ES6语法
33
+ 1). from + Set
34
+ 2). ... + Set
35
+ 说明: 编码简洁
36
+ */
37
+ export function unique3 (array) {
38
+ // return Array.from(new Set(array))
39
+ return [...new Set(array)]
40
+ }
@@ -0,0 +1,97 @@
1
+ /*
2
+ 1. 函数的返回值为promise, 成功的结果为response, 失败的结果为error
3
+ 2. 能处理多种类型的请求: GET/POST/PUT/DELETE
4
+ 3. 函数的参数为一个配置对象
5
+ {
6
+ url: '', // 请求地址
7
+ method: '', // 请求方式GET/POST/PUT/DELETE
8
+ params: {}, // GET/DELETE请求的query参数
9
+ data: {}, // POST或DELETE请求的请求体参数
10
+ }
11
+ 4. 响应json数据自动解析为js的对象/数组
12
+ */
13
+ /* 发送任意类型请求的函数 */
14
+ function axios({
15
+ url,
16
+ method='GET',
17
+ params={},
18
+ data={}
19
+ }) {
20
+ // 返回一个promise对象
21
+ return new Promise((resolve, reject) => {
22
+
23
+ // 处理method(转大写)
24
+ method = method.toUpperCase()
25
+
26
+ // 处理query参数(拼接到url上) id=1&xxx=abc
27
+ /*
28
+ {
29
+ id: 1,
30
+ xxx: 'abc'
31
+ }
32
+ */
33
+ let queryString = ''
34
+ Object.keys(params).forEach(key => {
35
+ queryString += `${key}=${params[key]}&`
36
+ })
37
+ if (queryString) { // id=1&xxx=abc&
38
+ // 去除最后的&
39
+ queryString = queryString.substring(0, queryString.length-1)
40
+ // 接到url
41
+ url += '?' + queryString
42
+ }
43
+
44
+
45
+ // 1. 执行异步ajax请求
46
+ // 创建xhr对象
47
+ const request = new XMLHttpRequest()
48
+ // 打开连接(初始化请求, 没有请求)
49
+ request.open(method, url, true)
50
+
51
+ // 发送请求
52
+ if (method==='GET') {
53
+ request.send()
54
+ } else if (method==='POST' || method==='PUT' || method==='DELETE'){
55
+ request.setRequestHeader('Content-Type', 'application/json;charset=utf-8') // 告诉服务器请求体的格式是json
56
+ request.send(JSON.stringify(data)) // 发送json格式请求体参数
57
+ }
58
+
59
+ // 绑定状态改变的监听
60
+ request.onreadystatechange = function () {
61
+ // 如果请求没有完成, 直接结束
62
+ if (request.readyState!==4) {
63
+ return
64
+ }
65
+ // 如果响应状态码在[200, 300)之间代表成功, 否则失败
66
+ const {status, statusText} = request
67
+ // 2.1. 如果请求成功了, 调用resolve()
68
+ if (status>=200 && status<=299) {
69
+ // 准备结果数据对象response
70
+ const response = {
71
+ data: JSON.parse(request.response),
72
+ status,
73
+ statusText
74
+ }
75
+ resolve(response)
76
+ } else { // 2.2. 如果请求失败了, 调用reject()
77
+ reject(new Error('request error status is ' + status))
78
+ }
79
+ }
80
+ })
81
+ }
82
+
83
+ /* 发送特定请求的静态方法 */
84
+ axios.get = function (url, options) {
85
+ return axios(Object.assign(options, {url, method: 'GET'}))
86
+ }
87
+ axios.delete = function (url, options) {
88
+ return axios(Object.assign(options, {url, method: 'DELETE'}))
89
+ }
90
+ axios.post = function (url, data, options) {
91
+ return axios(Object.assign(options, {url, data, method: 'POST'}))
92
+ }
93
+ axios.put = function (url, data, options) {
94
+ return axios(Object.assign(options, {url, data, method: 'PUT'}))
95
+ }
96
+
97
+ export default axios
@@ -0,0 +1,46 @@
1
+ const eventBus = {}
2
+
3
+ /*
4
+ {
5
+ add: [callback1, callback2]
6
+ delete: [callback3]
7
+ }
8
+ */
9
+ let callbacksObj = {}
10
+
11
+ /*
12
+ 绑定事件监听
13
+ */
14
+ eventBus.on = function (eventName, callback) {
15
+ const callbacks = callbacksObj[eventName]
16
+ if (callbacks) {
17
+ callbacks.push(callback)
18
+ } else {
19
+ callbacksObj[eventName] = [callback]
20
+ }
21
+ }
22
+
23
+ /*
24
+ 分发事件
25
+ */
26
+ eventBus.emit = function (eventName, data) {
27
+ const callbacks = callbacksObj[eventName]
28
+ if (callbacks && callbacks.length > 0) {
29
+ callbacks.forEach(callback => {
30
+ callback(data)
31
+ })
32
+ }
33
+ }
34
+
35
+ /*
36
+ 移除事件监听
37
+ */
38
+ eventBus.off = function (eventName) {
39
+ if (eventName) {
40
+ delete callbacksObj[eventName]
41
+ } else {
42
+ callbacksObj = {}
43
+ }
44
+ }
45
+
46
+ export default eventBus
@@ -0,0 +1,17 @@
1
+ /*
2
+ 自定义函数对象的apply方法
3
+ */
4
+ export function apply (fn, obj, args) {
5
+ // 如果传入的是null/undefined, this指定为window
6
+ if (obj===null || obj===undefined) {
7
+ obj = obj || window
8
+ }
9
+ // 给obj添加一个方法: 属性名任意, 属性值必须当前调用call的函数对象
10
+ obj.tempFn = fn
11
+ // 通过obj调用这个方法
12
+ const result = obj.tempFn(...args)
13
+ // 删除新添加的方法
14
+ delete obj.tempFn
15
+ // 返回函数调用的结果
16
+ return result
17
+ }
@@ -0,0 +1,18 @@
1
+ import {call} from './call'
2
+ /*
3
+ 自定义函数对象的bind方法
4
+ 重要技术:
5
+ 高阶函数
6
+ 闭包
7
+ call()
8
+ 三点运算符
9
+ */
10
+ export function bind (fn, obj, ...args) {
11
+ if (obj===null || obj===undefined) {
12
+ obj = obj || window
13
+ }
14
+
15
+ return function (...args2) {
16
+ call(fn, obj, ...args, ...args2)
17
+ }
18
+ }
@@ -0,0 +1,17 @@
1
+ /*
2
+ 自定义函数对象的call方法
3
+ */
4
+ export function call (fn, obj, ...args) {
5
+ // 如果传入的是null/undefined, this指定为window
6
+ if (obj===null || obj===undefined) {
7
+ obj = obj || window
8
+ }
9
+ // 给obj添加一个方法: 属性名任意, 属性值必须当前调用call的函数对象
10
+ obj.tempFn = fn
11
+ // 通过obj调用这个方法
12
+ const result = obj.tempFn(...args)
13
+ // 删除新添加的方法
14
+ delete obj.tempFn
15
+ // 返回函数调用的结果
16
+ return result
17
+ }
@@ -0,0 +1,22 @@
1
+ /*
2
+ 实现函数防抖的函数
3
+ */
4
+ export function debounce(callback, delay) {
5
+ return function () {
6
+ console.log('debounce 事件...')
7
+ // 保存this和arguments
8
+ const that = this
9
+ const args = arguments
10
+
11
+ // 清除待执行的定时器任务
12
+ if (callback.timeoutId) {
13
+ clearTimeout(callback.timeoutId)
14
+ }
15
+ // 每隔delay的时间, 启动一个新的延迟定时器, 去准备调用callback
16
+ callback.timeoutId = setTimeout(function () {
17
+ callback.apply(that, args)
18
+ // 如果定时器回调执行了, 删除标记
19
+ delete callback.timeoutId
20
+ }, delay)
21
+ }
22
+ }
@@ -0,0 +1,15 @@
1
+ /*
2
+ 实现函数节流的函数
3
+ */
4
+
5
+ export function throttle(callback, delay) {
6
+ let start = 0 // 必须保存第一次点击立即调用
7
+ return function () { // 它的this是谁就得让callback()中的this是谁, 它接收的所有实参都直接交给callback()
8
+ console.log('throttle 事件')
9
+ const current = Date.now()
10
+ if (current - start > delay) { // 从第2次点击开始, 需要间隔时间超过delay
11
+ callback.apply(this, arguments)
12
+ start = current
13
+ }
14
+ }
15
+ }
package/src/index.js ADDED
@@ -0,0 +1,30 @@
1
+ export { chunk, chunk2 } from './array/chunk'
2
+ export { compact } from './array/compact'
3
+ export { concat } from './array/concat'
4
+ export { map, filter, reduce, find, findIndex, every, some, test } from './array/declares'
5
+ export { difference } from './array/difference'
6
+ export { drop, dropRight } from './array/drop'
7
+ export { flatten1, flatten2 } from './array/flatten'
8
+ export { mergeArray } from './array/merge'
9
+ export { pull, pullAll } from './array/pull'
10
+ export { slice } from './array/slice'
11
+ export { unique1, unique2, unique3 } from './array/unique'
12
+
13
+ export { apply } from './function/apply'
14
+ export { bind } from './function/bind'
15
+ export { call } from './function/call'
16
+ export { debounce } from './function/debounce'
17
+ export { throttle } from './function/throttle'
18
+
19
+ export { clone1, clone2 } from './object/clone'
20
+ export { deepClone1, deepClone2, deepClone3, deepClone4 } from './object/deepClone'
21
+ export { mergeObject } from './object/merge'
22
+ export { myInstanceOf } from './object/myInstanceOf'
23
+ export { newInstance } from './object/newInstance'
24
+
25
+ export { reverseString, palindrome, truncate } from './string'
26
+
27
+ export { default as axios } from './axios'
28
+ export { default as eventBus } from './event-bus'
29
+ export { default as Promise } from './promise'
30
+ export { default as PubSub } from './pub-sub'
@@ -0,0 +1,34 @@
1
+ /*
2
+ 实现浅拷贝
3
+ 方法一: 利用ES6语法
4
+ 方法二: 利用ES5语法: for...in
5
+ */
6
+ /* 方法一: 利用ES6语法*/
7
+ export function clone1(target) {
8
+ if (target instanceof Array) {
9
+ // return target.slice()
10
+ // return target.filter(() => true)
11
+ // return target.map(item => item)
12
+ return [...target]
13
+ } else if (target instanceof Object){
14
+ // return Object.assign({}, target)
15
+ return {...target}
16
+ } else {
17
+ return target
18
+ }
19
+ }
20
+
21
+ /* 方法二: 利用ES5语法: for...in */
22
+ export function clone2(target) {
23
+ if (target!=null && typeof target==='object') {
24
+ const cloneTarget = Array.isArray(target) ? [] : {}
25
+ for (let key in target) {
26
+ if (target.hasOwnProperty(key)) {
27
+ cloneTarget[key] = target[key]
28
+ }
29
+ }
30
+ return cloneTarget
31
+ } else {
32
+ return target
33
+ }
34
+ }