Skip to content

Commit

Permalink
Merge branch 'master' into chore/event-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Apr 5, 2020
2 parents 1acdbad + b9ad3cb commit 54bc58e
Show file tree
Hide file tree
Showing 14 changed files with 1,142 additions and 475 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
@@ -0,0 +1,2 @@
open_collective: "pikax"
custom: "https://paypal.me/pikaxdev"
15 changes: 14 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,9 +7,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

---

## Changed
## Changes

- [event][https://pikax.me/vue-composable/composable/event/event.html] - improve typing
- [path](https://pikax.me/vue-composable/composable/format/path) - Improve [array path access](https://pikax.me/vue-composable/composable/format/path.html#access) and add dev warnings
- [i18n](https://pikax.me/vue-composable/composable/i18n/i18n) - Allow to have factory based locale messages
- [i18n](https://pikax.me/vue-composable/composable/i18n/i18n) - Added console warnings when removing locales
- [i18n](https://pikax.me/vue-composable/composable/i18n/i18n) - Improve overriding locales
- [i18n](https://pikax.me/vue-composable/composable/i18n/i18n) - Setting new locale if the current locale is removed

## Added

- [i18n](https://pikax.me/vue-composable/composable/i18n/i18n) - Added `$tc`, same as `$t` but returns a string, sugar for usage in the template.

## Fixes

- [useValidation] - Fix tracking of `$value` when is not `ref`

## 1.0.0-dev.16

Expand Down
33 changes: 33 additions & 0 deletions docs/composable/format/path.md
Expand Up @@ -35,6 +35,39 @@ const name = usePath<string>({ user: { name: "test" } }, "user.name");
| ----- | -------- | ------------------------------------------------- |
| name | `Ref<T>` | Readonly `ref` with the object value for the path |
## Access
```js
const o = {
a: {
a: 1,
b: [
2,
{
c: {
["a-b-c-d"]: 3
}
}
]
}
};

usePath(o, "a[a]"); // result: 1 | equivalent: a.a
usePath(o, "[a]['a']"); // result: 1 | equivalent: a.a
usePath(o, '["a"][`b`][0]'); // result: 2 | equivalent: a.b["0"]
usePath(o, "a.b[1].c[a-b-c-d]"); // result: 3 | equivalent: a.b[1].c["a-b-c-d"]
```
## Limitations
The access in `[]` is limited to this regex expression:
```regex
/\[[`'"]?([^`'"\]]*)[`'"]?\]/g
```
If you want to improve this, please raise an [issue](https://github.com/pikax/vue-composable/issues/new) or create a [PR](https://github.com/pikax/vue-composable/pulls)
## Example
<path-example/>
Expand Down
13 changes: 7 additions & 6 deletions docs/composable/i18n/i18n.md
Expand Up @@ -189,14 +189,15 @@ The `useI18n` function exposes the following methods:
```js
import { useI18n } from "vue-composable";

const { $t, addLocale, removeLocale } = useI18n();
const { $t, $ts, addLocale, removeLocale } = useI18n();
```

| Signature | Description |
| ----------------------------- | -------------------------- |
| `$t(path, args?)` | Retrieve localised message |
| `addLocale(locale, messages)` | add new locale |
| `removeLocale(locale)` | remove locale |
| Signature | Description |
| ----------------------------- | -------------------------------------------------------- |
| `$t(path, args?)` | Retrieve localised message |
| `$ts(path, args?)` | Same as `$t` but returns string instead of `ref<string>` |
| `addLocale(locale, messages)` | add new locale |
| `removeLocale(locale)` | remove locale |

## Example

Expand Down
16 changes: 8 additions & 8 deletions package.json
Expand Up @@ -63,35 +63,35 @@
]
},
"devDependencies": {
"@microsoft/api-extractor": "^7.7.10",
"@microsoft/api-extractor": "^7.7.12",
"@rollup/plugin-json": "^4.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"@rollup/plugin-replace": "^2.3.1",
"@types/jest": "^25.1.4",
"@types/node": "^13.9.4",
"@types/jest": "^25.1.5",
"@types/node": "^13.11.0",
"@vue/composition-api": "^0.5.0",
"@vuepress/plugin-back-to-top": "^1.4.0",
"@vuepress/plugin-pwa": "^1.4.0",
"axios": "^0.19.2",
"brotli": "^1.3.2",
"chalk": "^3.0.0",
"chalk": "^4.0.0",
"coveralls": "^3.0.11",
"enquirer": "^2.3.4",
"execa": "^4.0.0",
"fs-extra": "^9.0.0",
"husky": "^4.2.3",
"jest": "^25.2.3",
"jest": "^25.2.6",
"jest-junit": "^10.0.0",
"jest-websocket-mock": "^2.0.2",
"lint-staged": "^10.0.9",
"lint-staged": "^10.1.1",
"lodash.camelcase": "^4.3.0",
"minimist": "^1.2.5",
"mock-socket": "^9.0.3",
"rimraf": "^3.0.2",
"rollup": "^2.2.0",
"rollup": "^2.3.2",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.27.0",
"ts-jest": "^25.2.1",
"ts-jest": "^25.3.0",
"tsd": "^0.11.0",
"typescript": "^3.8.3",
"vue": "^2.6.10",
Expand Down
25 changes: 23 additions & 2 deletions packages/axios/__tests__/axios.spec.ts
Expand Up @@ -24,7 +24,7 @@ describe("axios", () => {
});

it("should call axios", async () => {
const { exec, client } = useAxios();
const { exec, client } = useAxios(false);
const request: AxiosRequestConfig = {
method: "GET",
url: "./api/1"
Expand All @@ -37,6 +37,20 @@ describe("axios", () => {
);
});

it("should call axios using string and options", async () => {
const url = "./api/1";
const request: AxiosRequestConfig = {
method: "GET"
};
const { exec, client } = useAxios(url, request);

await exec(url);

expect(client.value.request).toBeCalledWith(
expect.objectContaining({ url, ...request })
);
});

it("should call axios using string", async () => {
const { exec, client } = useAxios();
const url = "./api/1";
Expand Down Expand Up @@ -190,7 +204,7 @@ describe("axios", () => {
const req: Partial<AxiosRequestConfig> = {
url: "./api/1"
};
useAxios(req);
useAxios(req, false);
expect(request).toBeCalledWith(expect.objectContaining(req));
});

Expand All @@ -206,4 +220,11 @@ describe("axios", () => {
"Cannot cancel because no request has been made"
);
});

it("should warn if cancel is called before any request has been made", () => {
const { cancel } = useAxios(true);
expect(cancel).toThrowError(
"Cannot cancel because no request has been made"
);
});
});
103 changes: 103 additions & 0 deletions packages/core/__tests__/format/path.spec.ts
@@ -1,4 +1,5 @@
import { usePath } from "../../src";
import { ref } from "@vue/composition-api";

describe("path", () => {
it("should return the object value", () => {
Expand Down Expand Up @@ -35,4 +36,106 @@ describe("path", () => {
expect(usePath(o, "array[1]").value).toBe(2);
expect(usePath(o, "deep.x[1].a.b").value).toBe(1);
});

describe("not found", () => {
const notFoundResolverMock = jest.fn().mockImplementation(() => "test");

const warnSpy = jest.spyOn(console, "warn");

beforeEach(() => {
notFoundResolverMock.mockClear();
warnSpy.mockClear();
});

test("source `undefined`", () => {
expect(
usePath(ref(undefined), "yey", undefined, notFoundResolverMock).value
).toBe("test");
expect(notFoundResolverMock).toHaveBeenLastCalledWith(
"yey",
undefined,
"yey",
undefined
);
});

test("no path", () => {
const o = {
a: 1
};
expect(usePath(o, "", undefined, notFoundResolverMock).value).toBe(o);
expect(notFoundResolverMock).not.toBeCalled();
});

test("first path not found", () => {
const o = {
a: 1
};
expect(usePath(o, "b", undefined, notFoundResolverMock).value).toBe(
"test"
);
expect(notFoundResolverMock).toBeCalled();
expect(warnSpy).toBeCalledWith(`Path "b" doesn't exist on:`, o);
});

test("deep path not found", () => {
const o = {
a: {
c: "hello"
}
};
expect(usePath(o, "a.c.a", undefined, notFoundResolverMock).value).toBe(
"test"
);
expect(notFoundResolverMock).toBeCalled();
expect(warnSpy).toBeCalledWith(`Path "a.c.a" doesn't exist on:`, o);
});

test("if access with []", () => {
const o = {};
expect(usePath(o, "[]", undefined, notFoundResolverMock).value).toBe(
"test"
);
expect(warnSpy).toBeCalledWith(`Path "[]" doesn't exist on:`, o);
});

test("access with consecutive []", () => {
const o = {
a: {
a: 1,
b: [
2,
{
c: {
["a-b-c-d"]: 3
}
}
]
}
};

expect(usePath(o, "a[a]").value).toBe(o.a.a);
expect(usePath(o, "[a]['a']").value).toBe(o.a.a);
expect(usePath(o, '["a"][`b`][0]').value).toBe(o.a.b[0]);
expect(usePath(o, "a.b[1].c[a-b-c-d]").value).toBe(
(o.a.b[1] as any).c["a-b-c-d"]
);
});

test("invalid path parsing", () => {
expect(usePath({}, "a[a]o[a]").value).toBeUndefined();
expect(warnSpy).toHaveBeenNthCalledWith(
1,
`[usePath] invalid path "a[a]o[a]"`
);
});

test("invalid array accessor", () => {
expect(usePath({}, "aa]").value).toBeUndefined();
expect(warnSpy).toHaveBeenNthCalledWith(
1,
`[usePath] invalid path provided "aa]"`
);
});
});
});

0 comments on commit 54bc58e

Please sign in to comment.