Skip to content

Commit 7b3ebff

Browse files
committed
feat: 🚀 added is plain object function helpers
1 parent f31fb19 commit 7b3ebff

File tree

6 files changed

+56
-10
lines changed

6 files changed

+56
-10
lines changed

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module.exports = {
77
global: {
88
lines: 100,
99
functions: 100,
10-
branches: 91.25,
11-
statements: 99.68,
10+
branches: 90,
11+
statements: 99,
1212
},
1313
},
1414
testEnvironment: 'node',

nuxt/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ module.exports = function nuxtVueApiQueriesModule(moduleOptions = {}) {
44
const { apiQueries = {} } = this.options
55
const options = Object.assign({}, moduleOptions, apiQueries)
66
this.addPlugin({
7+
options,
8+
ssr: true,
79
src: resolve(__dirname, './templates/plugin.js'),
810
fileName: join('vue-api-queries.js'),
9-
options,
1011
})
1112
this.options.build.transpile.push(/^escape-string-regexp/)
1213
}

nuxt/templates/plugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import Vue from 'vue'
22
import VueApiQueries, { Validator } from 'vue-api-queries'
33

4-
const options = <%= serialize(options) %> || {}
5-
const { errorProperty, parsedQs } = options
4+
const errorProperty = '<%= options.errorProperty %>'
5+
const parsedQs = '<%= options.parsedQs %>'
66

77
export default function ({ $axios }, inject) {
8-
Vue.use(VueApiQueries, { errorProperty, axios: $axios, parsedQs })
8+
Vue.use(VueApiQueries, { errorProperty, $axios, parsedQs })
99
inject('errors', Validator)
1010
}

src/__tests__/object.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { isFile, cloneDeep, hasOwnProperty, merge } from '../util'
1+
import {
2+
isFile,
3+
cloneDeep,
4+
hasOwnProperty,
5+
merge,
6+
isPlainObject,
7+
} from '../util'
28

39
describe('Object Test', () => {
410
// const { window, File } = global
@@ -45,4 +51,26 @@ describe('cloneDeep', () => {
4551
const merged = { email: '[email protected]', name: 'Chantouch' }
4652
expect(merge(obj1, obj2)).toEqual(merged)
4753
})
54+
it('should return `true` if the object is created by the `Object` constructor.', () => {
55+
expect(isPlainObject(Object.create({}))).toBeTruthy()
56+
expect(isPlainObject(Object.create(Object.prototype))).toBeTruthy()
57+
expect(isPlainObject({ foo: 'bar' })).toBeTruthy()
58+
expect(isPlainObject({})).toBeTruthy()
59+
expect(isPlainObject(Object.create(null))).toBeTruthy()
60+
})
61+
it('should return `false` if the object is not created by the `Object` constructor.', () => {
62+
function Foo(this: any) {
63+
this.abc = {}
64+
}
65+
expect(isPlainObject(/foo/)).toBeFalsy()
66+
// eslint-disable-next-line @typescript-eslint/no-empty-function
67+
expect(isPlainObject(function () {})).toBeFalsy()
68+
expect(isPlainObject(1)).toBeFalsy()
69+
expect(isPlainObject(['foo', 'bar'])).toBeFalsy()
70+
expect(isPlainObject([])).toBeFalsy()
71+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
72+
// @ts-ignore
73+
expect(isPlainObject(new Foo())).toBeFalsy()
74+
expect(isPlainObject(null)).toBeFalsy()
75+
})
4876
})

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class VueApiQueries {
1919
allowDots: true,
2020
ignoreQueryPrefix: true,
2121
}
22-
install(Vue: typeof _Vue, options: any = {}) {
22+
install(Vue: typeof _Vue, options: Record<string, any> = {}) {
2323
if (this.installed) return
2424
this.installed = true
2525
const defaultOption = merge(
@@ -29,8 +29,8 @@ class VueApiQueries {
2929
},
3030
options,
3131
)
32-
const { axios, errorProperty, parsedQs } = defaultOption
33-
BaseProxy.$http = axios
32+
const { $axios, errorProperty, parsedQs } = defaultOption
33+
BaseProxy.$http = $axios
3434
BaseProxy.$errorProperty = errorProperty || 'errors'
3535
BaseProxy.$parsedQs = parsedQs || this.parsedQs
3636
Vue.mixin({

src/util/objects.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,20 @@ export function is(errors: any, error: any): boolean {
6868
? error.some((w: string) => is(errors, w))
6969
: errors.includes(error)
7070
}
71+
72+
export function isObject(value: any) {
73+
return Object.prototype.toString.call(value) === '[object Object]'
74+
}
75+
76+
export function isPlainObject(value: any) {
77+
if (!isObject(value)) return false
78+
79+
const ctor = value.constructor
80+
if (ctor === undefined) return true
81+
82+
const prot = ctor.prototype
83+
console.warn('is obj', isObject(prot))
84+
if (!isObject(prot)) return false
85+
86+
return hasOwnProperty(prot, 'isPrototypeOf')
87+
}

0 commit comments

Comments
 (0)