@antdv/pro-utils
Version:
@antdv/pro-utils
149 lines (148 loc) • 4.21 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
until: () => until
});
module.exports = __toCommonJS(stdin_exports);
var import_vue = require("vue");
var import_utils = require("../../vueuse/utils");
function createUntil(r, isNot = false) {
function toMatch(condition, { flush = "sync", deep = false, timeout, throwOnTimeout } = {}) {
let stop = null;
const watcher = new Promise((resolve) => {
stop = (0, import_vue.watch)(
r,
(v) => {
if (condition(v) !== isNot) {
if (stop)
stop();
else
(0, import_vue.nextTick)(() => stop == null ? void 0 : stop());
resolve(v);
}
},
{
flush,
deep,
immediate: true
}
);
});
const promises = [watcher];
if (timeout != null) {
promises.push(
(0, import_utils.promiseTimeout)(timeout, throwOnTimeout).then(() => (0, import_utils.toValue)(r)).finally(() => stop == null ? void 0 : stop())
);
}
return Promise.race(promises);
}
function toBe(value, options) {
if (!(0, import_vue.isRef)(value))
return toMatch((v) => v === value, options);
const { flush = "sync", deep = false, timeout, throwOnTimeout } = options != null ? options : {};
let stop = null;
const watcher = new Promise((resolve) => {
stop = (0, import_vue.watch)(
[r, value],
([v1, v2]) => {
if (isNot !== (v1 === v2)) {
if (stop)
stop();
else
(0, import_vue.nextTick)(() => stop == null ? void 0 : stop());
resolve(v1);
}
},
{
flush,
deep,
immediate: true
}
);
});
const promises = [watcher];
if (timeout != null) {
promises.push(
(0, import_utils.promiseTimeout)(timeout, throwOnTimeout).then(() => (0, import_utils.toValue)(r)).finally(() => {
stop == null ? void 0 : stop();
return (0, import_utils.toValue)(r);
})
);
}
return Promise.race(promises);
}
function toBeTruthy(options) {
return toMatch((v) => Boolean(v), options);
}
function toBeNull(options) {
return toBe(null, options);
}
function toBeUndefined(options) {
return toBe(void 0, options);
}
function toBeNaN(options) {
return toMatch(Number.isNaN, options);
}
function toContains(value, options) {
return toMatch((v) => {
const array = Array.from(v);
return array.includes(value) || array.includes((0, import_utils.toValue)(value));
}, options);
}
function changed(options) {
return changedTimes(1, options);
}
function changedTimes(n = 1, options) {
let count = -1;
return toMatch(() => {
count += 1;
return count >= n;
}, options);
}
if (Array.isArray((0, import_utils.toValue)(r))) {
const instance = {
toMatch,
toContains,
changed,
changedTimes,
get not() {
return createUntil(r, !isNot);
}
};
return instance;
} else {
const instance = {
toMatch,
toBe,
toBeTruthy,
toBeNull,
toBeNaN,
toBeUndefined,
changed,
changedTimes,
get not() {
return createUntil(r, !isNot);
}
};
return instance;
}
}
function until(r) {
return createUntil(r);
}