|
1 |
| -const isPlainObject = require('lodash.isplainobject') |
2 |
| - |
3 | 1 | const parser = require('postcss-selector-parser')
|
4 | 2 | const parseSelector = parser()
|
5 | 3 |
|
6 | 4 | module.exports = {
|
| 5 | + isObject(value) { |
| 6 | + return typeof value === 'object' && value !== null |
| 7 | + }, |
| 8 | + |
| 9 | + isPlainObject(value) { |
| 10 | + if (typeof value !== 'object' || value === null) { |
| 11 | + return false |
| 12 | + } |
| 13 | + |
| 14 | + if (Object.prototype.toString.call(value) !== '[object Object]') { |
| 15 | + return false |
| 16 | + } |
| 17 | + |
| 18 | + if (Object.getPrototypeOf(value) === null) { |
| 19 | + return true |
| 20 | + } |
| 21 | + |
| 22 | + let proto = value |
| 23 | + while (Object.getPrototypeOf(proto) !== null) { |
| 24 | + proto = Object.getPrototypeOf(proto) |
| 25 | + } |
| 26 | + |
| 27 | + return Object.getPrototypeOf(value) === proto |
| 28 | + }, |
| 29 | + |
| 30 | + merge(target, ...sources) { |
| 31 | + if (!sources.length) return target |
| 32 | + const source = sources.shift() |
| 33 | + |
| 34 | + if (this.isObject(target) && this.isObject(source)) { |
| 35 | + for (const key in source) { |
| 36 | + if (Array.isArray(source[key])) { |
| 37 | + if (!target[key]) target[key] = [] |
| 38 | + source[key].forEach((item, index) => { |
| 39 | + if (this.isPlainObject(item) && this.isPlainObject(target[key][index])) { |
| 40 | + target[key][index] = this.merge(target[key][index], item) |
| 41 | + } else { |
| 42 | + target[key][index] = item |
| 43 | + } |
| 44 | + }) |
| 45 | + } else if (this.isPlainObject(source[key])) { |
| 46 | + if (!target[key]) target[key] = {} |
| 47 | + this.merge(target[key], source[key]) |
| 48 | + } else { |
| 49 | + target[key] = source[key] |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return this.merge(target, ...sources) |
| 55 | + }, |
| 56 | + |
| 57 | + castArray(value) { |
| 58 | + return Array.isArray(value) ? value : [value] |
| 59 | + }, |
| 60 | + |
7 | 61 | isUsableColor(color, values) {
|
8 |
| - return isPlainObject(values) && color !== 'gray' && values[600] |
| 62 | + return this.isPlainObject(values) && color !== 'gray' && values[600] |
9 | 63 | },
|
10 | 64 |
|
11 | 65 | /**
|
|
0 commit comments