Skip to content

Commit be0605f

Browse files
committed
chore: fix code style
1 parent c1bfd93 commit be0605f

35 files changed

+255
-203
lines changed

.coveralls.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
service_name: travis-pro
2-
repo_token: zaQVFPoDNH8C69eCILMzPzi9qhZEm1Dws
2+
repo_token: zaQVFPoDNH8C69eCILMzPzi9qhZEm1Dws

.github/workflows/node.js.yml

+21-22
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@
44
name: CI
55

66
on:
7-
push:
8-
branches: [ "main" ]
9-
pull_request:
10-
branches: [ "main" ]
7+
push:
8+
branches: ['main']
9+
pull_request:
10+
branches: ['main']
1111

1212
jobs:
13-
build:
13+
build:
14+
runs-on: ubuntu-latest
1415

15-
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
node-version: [16.x]
19+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1620

17-
strategy:
18-
matrix:
19-
node-version: [16.x]
20-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21-
22-
steps:
23-
- uses: actions/checkout@v3
24-
- name: Use Node.js ${{ matrix.node-version }}
25-
uses: actions/setup-node@v3
26-
# with:
27-
# node-version: ${{ matrix.node-version }}
28-
# cache: 'npm'
29-
- run: yarn
30-
- run: yarn build
31-
- run: yarn coverage
32-
- run: yarn coveralls
21+
steps:
22+
- uses: actions/checkout@v3
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v3
25+
# with:
26+
# node-version: ${{ matrix.node-version }}
27+
# cache: 'npm'
28+
- run: yarn
29+
- run: yarn build
30+
- run: yarn coverage
31+
- run: yarn coveralls

.prettierignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
coverage
3+
dist
4+
node_modules

.prettierrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
trailingComma: 'es5',
3+
tabWidth: 4,
4+
semi: true,
5+
singleQuote: true,
6+
};

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
[![Build Status](https://github.com/fmfe/vue-class-setup/workflows/CI/badge.svg)](https://github.com/fmfe/vue-class-setup/actions)
44
<a href='https://coveralls.io/github/fmfe/vue-class-setup?branch=main'><img src='https://coveralls.io/repos/github/fmfe/vue-class-setup/badge.svg?branch=main' alt='Coverage Status' /></a>
5-
[![npm](https://img.shields.io/npm/v/vue-class-setup.svg)](https://www.npmjs.com/package/vue-class-setup)
5+
[![npm](https://img.shields.io/npm/v/vue-class-setup.svg)](https://www.npmjs.com/package/vue-class-setup)
66
[![npm](https://img.shields.io/npm/dm/vue-class-setup.svg)](https://www.npmjs.com/package/vue-class-setup)
77
[![npm](https://img.shields.io/npm/dt/vue-class-setup.svg)](https://www.npmjs.com/package/vue-class-setup)
88

99
## Why?
10+
1011
Using class can help you avoid `ref`, `reactive` and `computed`, and significantly reduce your mental burden and better organize your code. It supports vue2 and vue3 at the same time. After gzip compression, it is only 1KB
1112

1213
## Install
14+
1315
```bash
1416
npm install vue-class-setup
1517
# or
1618
yarn add vue-class-setup
1719
```
1820

1921
## Quick start
22+
2023
```vue
2124
<script lang="ts">
2225
import { onMounted } from 'vue';
@@ -36,7 +39,6 @@ class Count {
3639
this.value++;
3740
}
3841
}
39-
4042
</script>
4143
<script setup lang="ts">
4244
// Use the class you write in setup
@@ -47,3 +49,17 @@ const count = new Count();
4749
<input type="number" v-model="count.text" />
4850
</template>
4951
```
52+
53+
## Custom setup
54+
55+
```ts
56+
import { Setup, Hook, onSetup } from 'vue-class-setup';
57+
58+
@Setup
59+
class Count {
60+
@PassOnTo(onSetup)
61+
private setup() {
62+
// Your code
63+
}
64+
}
65+
```

index.html

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>vue-class-setup</title>
7-
</head>
8-
<body>
9-
<div id="app"></div>
10-
<script type="module" src="/src/index.ts"></script>
11-
</body>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>vue-class-setup</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script type="module" src="/src/index.ts"></script>
11+
</body>
1212
</html>

package.json

+41-39
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
{
2-
"name": "vue-class-setup",
3-
"version": "0.1.4",
4-
"main": "dist/vue-class-setup.cjs.js",
5-
"module": "dist/vue-class-setup.es.js",
6-
"types": "dist/index.d.ts",
7-
"repository": "[email protected]:fmfe/vue-class-setup.git",
8-
"author": "<[email protected]>",
9-
"license": "MIT",
10-
"description": "Write setup using class",
11-
"keywords": [
12-
"vue-class",
13-
"vue-class-component",
14-
"vue-property-decorator"
15-
],
16-
"scripts": {
17-
"dev": "vite",
18-
"build": "vue-tsc --noEmit && vite build",
19-
"preview": "vite preview",
20-
"test": "vitest",
21-
"coverage": "vitest run --coverage",
22-
"coveralls": "coveralls < coverage/lcov.info",
23-
"release": "yarn build && npm publish --registry=https://registry.npmjs.org"
24-
},
25-
"peerDependencies": {
26-
"vue": ">=2.7.8 || >=3.0.0"
27-
},
28-
"devDependencies": {
29-
"@vitejs/plugin-vue": "^3.0.1",
30-
"@vue/test-utils": "^2.0.2",
31-
"c8": "^7.12.0",
32-
"coveralls": "^3.1.1",
33-
"happy-dom": "^6.0.4",
34-
"typescript": "^4.7.4",
35-
"vite": "^3.0.3",
36-
"vite-plugin-dts": "^1.4.0",
37-
"vitest": "^0.20.2",
38-
"vue": "^3.2.37",
39-
"vue-tsc": "^0.39.4"
40-
}
2+
"name": "vue-class-setup",
3+
"version": "0.1.4",
4+
"main": "dist/vue-class-setup.cjs.js",
5+
"module": "dist/vue-class-setup.es.js",
6+
"types": "dist/index.d.ts",
7+
"repository": "[email protected]:fmfe/vue-class-setup.git",
8+
"author": "<[email protected]>",
9+
"license": "MIT",
10+
"description": "Write setup using class",
11+
"keywords": [
12+
"vue-class",
13+
"vue-class-component",
14+
"vue-property-decorator"
15+
],
16+
"scripts": {
17+
"lint": "prettier --write .",
18+
"dev": "vite",
19+
"build": "vue-tsc --noEmit && vite build",
20+
"preview": "vite preview",
21+
"test": "vitest",
22+
"coverage": "vitest run --coverage",
23+
"coveralls": "coveralls < coverage/lcov.info",
24+
"release": "yarn build && npm publish --registry=https://registry.npmjs.org"
25+
},
26+
"peerDependencies": {
27+
"vue": ">=2.7.8 || >=3.0.0"
28+
},
29+
"devDependencies": {
30+
"@vitejs/plugin-vue": "^3.0.1",
31+
"@vue/test-utils": "^2.0.2",
32+
"c8": "^7.12.0",
33+
"coveralls": "^3.1.1",
34+
"happy-dom": "^6.0.4",
35+
"prettier": "^2.7.1",
36+
"typescript": "^4.7.4",
37+
"vite": "^3.0.3",
38+
"vite-plugin-dts": "^1.4.0",
39+
"vitest": "^0.20.2",
40+
"vue": "^3.2.37",
41+
"vue-tsc": "^0.39.4"
42+
}
4143
}

src/context.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { TargetName, Target } from './types';
33
let currentTarget: Target | null = null;
44
let currentName: TargetName | null = null;
55

6-
export function getCurrentHookContext(): { target: object, name: TargetName } {
6+
export function getCurrentHookContext(): { target: object; name: TargetName } {
77
if (currentTarget === null || currentName === null) {
88
throw new Error('Can only be obtained in hook functions');
99
}
10-
return { target: currentTarget, name: currentName }
10+
return { target: currentTarget, name: currentName };
1111
}
1212

1313
export function setCurrentHookTarget(target: Target | null) {

src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export { Setup } from './setup';
2-
export { onSetup } from './on-setup';
32
export { getCurrentHookContext } from './context';
4-
export { PassOnTo } from './pass-on-to';
3+
export { PassOnTo } from './pass-on-to';

src/on-computed.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { computed } from 'vue';
22
import { Target, TargetName } from './types';
33
import { getCurrentHookContext } from './context';
44

5-
function compute(target: Target, name: TargetName, descriptor: PropertyDescriptor, type: 'get' | 'value') {
5+
function compute(
6+
target: Target,
7+
name: TargetName,
8+
descriptor: PropertyDescriptor,
9+
type: 'get' | 'value'
10+
) {
611
if (typeof descriptor[type] !== 'function') return;
712

813
const value = descriptor[type].bind(target);
@@ -11,12 +16,14 @@ function compute(target: Target, name: TargetName, descriptor: PropertyDescripto
1116
...descriptor,
1217
[type]() {
1318
return compute.value;
14-
}
19+
},
1520
});
1621
}
1722

18-
19-
function getDescriptor(target: object, name: TargetName): PropertyDescriptor | null {
23+
function getDescriptor(
24+
target: object,
25+
name: TargetName
26+
): PropertyDescriptor | null {
2027
const descriptor = Object.getOwnPropertyDescriptor(target, name);
2128
if (descriptor) {
2229
return descriptor;
@@ -25,9 +32,8 @@ function getDescriptor(target: object, name: TargetName): PropertyDescriptor | n
2532
return getDescriptor(next, name);
2633
}
2734

28-
2935
export function onComputed() {
3036
const { target, name } = getCurrentHookContext();
3137
const descriptor = getDescriptor(target, name)!;
3238
compute(target, name, descriptor, 'get');
33-
}
39+
}

src/on-setup.ts

-3
This file was deleted.

src/options.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { TargetConstructorOptions, PassOnToCallback, TargetName } from './types';
1+
import {
2+
TargetConstructorOptions,
3+
PassOnToCallback,
4+
TargetName,
5+
} from './types';
26
import { SETUP_NAME, SETUP_OPTIONS_NAME } from './config';
37
let currentOptions: TargetConstructorOptions = new Map();
48

59
function getCurrentOptions() {
6-
return currentOptions
10+
return currentOptions;
711
}
812

913
function resetCurrentOptions() {
10-
currentOptions = new Map()
14+
currentOptions = new Map();
1115
}
1216

1317
export function getOptions(Target: any): TargetConstructorOptions {
@@ -37,15 +41,15 @@ export function getSetupOptions(Target: any) {
3741
const newNames = [...names];
3842
const tempName = temp.get(hook);
3943
if (tempName) {
40-
tempName.forEach(name => {
44+
tempName.forEach((name) => {
4145
if (!newNames.includes(name)) {
4246
newNames.push(name);
4347
}
4448
});
4549
}
4650
temp.set(hook, newNames);
4751
});
48-
resetCurrentOptions()
52+
resetCurrentOptions();
4953

5054
return temp;
5155
}

src/pass-on-to.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
import { PassOnToCallback, TargetName } from './types';
1+
import { TargetName, PassOnToCallback } from './types';
22
import { setOptions } from './options';
33

4-
export function PassOnTo(cb: PassOnToCallback) {
5-
return function (target: object, name: TargetName, descriptor: PropertyDescriptor) {
6-
setOptions(cb, name);
7-
}
4+
function onSetup(cb: () => void) {
5+
cb();
86
}
7+
8+
function PassOnTo<T extends (...args: any[]) => any>(
9+
cb: PassOnToCallback<T> = onSetup
10+
) {
11+
return function name(
12+
target: object,
13+
name: TargetName,
14+
descriptor: TypedPropertyDescriptor<T>
15+
) {
16+
setOptions(cb as any, name);
17+
};
18+
}
19+
20+
export { PassOnTo };

0 commit comments

Comments
 (0)