caring-route 0.0.3 → 0.0.5

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.
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ function t(t={},e=!0,r="brackets"){let a=e?"?":"",o=[];-1==["indices","brackets","repeat","comma"].indexOf(r)&&(r="brackets");for(let e in t){let a=t[e];if(!(["",void 0,null].indexOf(a)>=0))if(a.constructor===Array)switch(r){case"indices":for(let t=0;t<a.length;t++)o.push(e+"["+t+"]="+a[t]);break;case"brackets":default:a.forEach((t=>{o.push(e+"[]="+t)}));break;case"repeat":a.forEach((t=>{o.push(e+"="+t)}));break;case"comma":let t="";a.forEach((e=>{t+=(t?",":"")+e})),o.push(e+"="+t)}else o.push(e+"="+a)}return o.length?a+o.join("&"):""}const e=["navigateTo","redirectTo","reLaunch"],r=["navigateTo","redirectTo","reLaunch","navigateBack","switchTab"];function a(t){const{routeUrl:e,routeType:r="navigateTo",delta:a=1,events:o}=t;return new Promise(((t,n)=>{uni[r]({url:e,delta:a,events:o,success:e=>t(e),fail:t=>n(t)})}))}class o{handleParams(t={}){const e={};for(let r in t)"object"==typeof t[r]&&(e[r]=encodeURIComponent(JSON.stringify(t[r]))),"string"==typeof t[r]&&t[r].includes("http")&&(t[r]=encodeURIComponent(t[r]));return Object.assign(t,e)}query(t){const e={};for(const r in t)if("object"!=typeof t[r]){const a=decodeURIComponent(t[r]);(a.startsWith("{")||a.startsWith("["))&&"string"==typeof a&&(e[r]=JSON.parse(a))}return Object.assign(t,e)}navigate(t,e){return!!t&&("string"==typeof t?(e||(e={}),e.routeUrl=this.mixinParam(t,e),a(e)):(o=t,"[object Object]"===toString.call(o)?(e=t,r.includes(e.type)&&"string"==typeof e.url?(e.routeUrl=t.url,e.routeType=t.type,Reflect.deleteProperty(e,"url"),Reflect.deleteProperty(e,"type"),e.routeUrl=this.mixinParam(e.routeUrl,e.params),a(e)):this.query(e)):void 0));var o}mixinParam(e,r){e=e&&this.addRootPath(e);let a="";return/.*\/.*\?.*=.*/.test(e)?(a=t(this.handleParams(r),!1),e+"&"+a):(a=t(this.handleParams(r)),e+a)}to(t,e){const r={routeType:"navigateTo",routeUrl:t,params:e};this._navigateMethod(r)}back(t){const e={routeType:"navigateBack",delta:t};this._navigateMethod(e)}tab(t){const e={routeType:"switchTab",routeUrl:t};this._navigateMethod(e)}direct(t,e){const r={routeType:"redirectTo",routeUrl:t,params:e};this._navigateMethod(r)}launch(t,e){const r={routeType:"reLaunch",routeUrl:t,params:e};this._navigateMethod(r)}_navigateMethod(t){return e.includes(t.routeType)?t.routeUrl=this.mixinParam(t.routeUrl,t.params):t.routeUrl=t.routeUrl&&this.addRootPath(t.routeUrl),a(t)}addRootPath(t){return"/"===t[0]?t:`/${t}`}}var n=function(){const t=new o,e=o.prototype.navigate.bind(t);return Object.setPrototypeOf(e,t),e}();export{n as default};
package/package.json CHANGED
@@ -1,12 +1,21 @@
1
1
  {
2
2
  "name": "caring-route",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
6
9
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
10
+ "test": "echo \"Error: no test specified\" && exit 1",
11
+ "build": "rollup -c"
8
12
  },
13
+ "type": "module",
9
14
  "keywords": [],
10
15
  "author": "风度万人迷",
11
- "license": "ISC"
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "@rollup/plugin-terser": "^0.4.1",
19
+ "rollup": "^3.21.5"
20
+ }
12
21
  }
package/helpers/util.js DELETED
@@ -1,55 +0,0 @@
1
- export function isPlainObject(val) {
2
- return toString.call(val) === '[object Object]'
3
- }
4
-
5
- export function queryParams(data = {}, isPrefix = true, arrayFormat = 'brackets') {
6
- let prefix = isPrefix ? '?' : ''
7
- let _result = []
8
- if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets';
9
- for (let key in data) {
10
- let value = data[key]
11
- // 去掉为空的参数
12
- if (['', undefined, null].indexOf(value) >= 0) {
13
- continue;
14
- }
15
- // 如果值为数组,另行处理
16
- if (value.constructor === Array) {
17
- // e.g. {ids: [1, 2, 3]}
18
- switch (arrayFormat) {
19
- case 'indices':
20
- // 结果: ids[0]=1&ids[1]=2&ids[2]=3
21
- for (let i = 0; i < value.length; i++) {
22
- _result.push(key + '[' + i + ']=' + value[i])
23
- }
24
- break;
25
- case 'brackets':
26
- // 结果: ids[]=1&ids[]=2&ids[]=3
27
- value.forEach(_value => {
28
- _result.push(key + '[]=' + _value)
29
- })
30
- break;
31
- case 'repeat':
32
- // 结果: ids=1&ids=2&ids=3
33
- value.forEach(_value => {
34
- _result.push(key + '=' + _value)
35
- })
36
- break;
37
- case 'comma':
38
- // 结果: ids=1,2,3
39
- let commaStr = "";
40
- value.forEach(_value => {
41
- commaStr += (commaStr ? "," : "") + _value;
42
- })
43
- _result.push(key + '=' + commaStr)
44
- break;
45
- default:
46
- value.forEach(_value => {
47
- _result.push(key + '[]=' + _value)
48
- })
49
- }
50
- } else {
51
- _result.push(key + '=' + value)
52
- }
53
- }
54
- return _result.length ? prefix + _result.join('&') : ''
55
- }
package/index.js DELETED
@@ -1,171 +0,0 @@
1
- /**
2
- * 对象转url参数
3
- * @param {*} data,对象
4
- * @param {*} isPrefix,是否自动加上"?"
5
- */
6
- import {
7
- queryParams,
8
- isPlainObject
9
- } from './helpers/util.js'
10
-
11
- const PARAMS_ROUTE = ['navigateTo', 'redirectTo', 'reLaunch']
12
- const ROUTE_SET = ['navigateTo', 'redirectTo', 'reLaunch', 'navigateBack', 'switchTab']
13
-
14
- // 放在Route类里也可以
15
- function dispatchNavigate(config) {
16
- const {
17
- routeUrl: url,
18
- routeType = 'navigateTo',
19
- delta = 1,
20
- events
21
- } = config
22
-
23
- return new Promise((resolve, reject) => {
24
- uni[routeType]({
25
- url,
26
- delta,
27
- events,
28
- success: e => resolve(e),
29
- fail: e => reject(e),
30
- })
31
- })
32
- }
33
-
34
- class Route {
35
- handleParams(params = {}) {
36
- const obj = {}
37
- for (let key in params) {
38
- if (typeof params[key] === 'object') {
39
- obj[key] = encodeURIComponent(JSON.stringify(params[key]))
40
- }
41
- // 如果包含http链接进行编码操作
42
- if (typeof params[key] === 'string' && params[key].includes('http')) {
43
- params[key] = encodeURIComponent(params[key])
44
- }
45
- }
46
- return Object.assign(params, obj)
47
- }
48
- query(query) {
49
- const obj = {}
50
- for (const key in query) {
51
- // 非对象数据才会处理
52
- if (typeof query[key] !== 'object') {
53
- const q = decodeURIComponent(query[key])
54
- if (q.startsWith('{') || q.startsWith('[')) {
55
- if (typeof q === 'string') {
56
- obj[key] = JSON.parse(q)
57
- }
58
- }
59
- }
60
- }
61
- return Object.assign(query, obj)
62
- }
63
- navigate(url, config) {
64
- if (!url) return false
65
- if (typeof url === 'string') {
66
- // 如果url为字符串,则config为params, 即route(url, params)的形式
67
- if (!config) config = {}
68
- config.routeUrl = this.mixinParam(url, config)
69
- return dispatchNavigate(config)
70
- }
71
- if (isPlainObject(url)) {
72
- config = url
73
- // config中参数中包含type和url参数,表示这是一个请求
74
- if (ROUTE_SET.includes(config.type) && typeof config.url === 'string') {
75
- // 请求
76
- config.routeUrl = url.url
77
- config.routeType = url.type
78
- Reflect.deleteProperty(config, 'url')
79
- Reflect.deleteProperty(config, 'type')
80
- config.routeUrl = this.mixinParam(config.routeUrl, config.params)
81
- return dispatchNavigate(config)
82
- } else {
83
- // 解码路由参数
84
- return this.query(config)
85
- }
86
- }
87
- }
88
- // 整合路由参数
89
- mixinParam(url, params) {
90
- url = url && this.addRootPath(url)
91
-
92
- // 使用正则匹配,主要依据是判断是否有"/","?","="等,如“/page/index/index?name=mary"
93
-
94
- let query = ''
95
- if (/.*\/.*\?.*=.*/.test(url)) {
96
- // object对象转为get类型的参数
97
- query = queryParams(this.handleParams(params), false) // 如果有url中有get参数,转换后无需带上"?"
98
- // 因为已有get参数,所以后面拼接的参数需要带上"&"隔开
99
- return (url += '&' + query)
100
- } else {
101
- // 直接拼接参数,因为此处url中没有后面的query参数,也就没有"?/&"之类的符号
102
- query = queryParams(this.handleParams(params))
103
- return (url += query)
104
- }
105
- }
106
-
107
- to(routeUrl, params) {
108
- const config = {
109
- routeType: 'navigateTo',
110
- routeUrl,
111
- params
112
- }
113
- this._navigateMethod(config)
114
- }
115
-
116
- back(delta) {
117
- const config = {
118
- routeType: 'navigateBack',
119
- delta
120
- }
121
- this._navigateMethod(config)
122
- }
123
-
124
- tab(routeUrl) {
125
- const config = {
126
- routeType: 'switchTab',
127
- routeUrl
128
- }
129
- this._navigateMethod(config)
130
- }
131
-
132
- direct(routeUrl, params) {
133
- const config = {
134
- routeType: 'redirectTo',
135
- routeUrl,
136
- params
137
- }
138
- this._navigateMethod(config)
139
- }
140
-
141
- launch(routeUrl, params) {
142
- const config = {
143
- routeType: 'reLaunch',
144
- routeUrl,
145
- params
146
- }
147
- this._navigateMethod(config)
148
- }
149
- _navigateMethod(config) {
150
- if (PARAMS_ROUTE.includes(config.routeType)) {
151
- config.routeUrl = this.mixinParam(config.routeUrl, config.params)
152
- } else {
153
- config.routeUrl = config.routeUrl && this.addRootPath(config.routeUrl)
154
- }
155
- return dispatchNavigate(config)
156
- }
157
- // 判断url前面是否有"/",如果没有则加上,否则无法跳转
158
- addRootPath(url) {
159
- return url[0] === '/' ? url : `/${url}`
160
- }
161
- }
162
-
163
- function createRoute() {
164
- const context = new Route()
165
- const instance = Route.prototype.navigate.bind(context)
166
- // 关键,将Route的实例context上的原型赋值给instance,从而让instance具备Route的原型方法,instance是一个混合函数,既可以作为函数调用,也可以调用类的方法
167
- Object.setPrototypeOf(instance, context)
168
- return instance
169
- }
170
-
171
- export default createRoute()