Download code/test/JavaScript/0033336_rule.js from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/test/JavaScript/0033336_rule.js
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/code/test/JavaScript/0033336_rule.js
-
curl -L -o 0033336_rule.js https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/test/JavaScript/0033336_rule.js
1.99 kB
| import Vue from 'vue' | |
| import { isObject, isFunction } from 'lodash' | |
| import required from './rules/required' | |
| import maxLength from './rules/maxLength' | |
| import minLength from './rules/minLength' | |
| import max from './rules/max' | |
| import min from './rules/min' | |
| import numeric from './rules/numeric' | |
| import pattern from './rules/pattern' | |
| import type from './type' | |
| /** | |
| * 变量匹配正则 | |
| * | |
| * @type {RegExp} | |
| */ | |
| const ruleValueRe = /\$?\{ruleValue\}/g | |
| const valueRe = /\$?\{value\}/g | |
| export class Rule { | |
| constructor () { | |
| this.ruleValidators = new Vue({ | |
| data: { | |
| required, | |
| maxLength, | |
| minLength, | |
| max, | |
| min, | |
| numeric, | |
| pattern | |
| } | |
| }) | |
| } | |
| validate (val, rules, context) { | |
| if (!rules || !rules.length) { | |
| return true | |
| } | |
| rules = Array.isArray(rules) ? rules : [rules] | |
| let contextData = type.clone(context) | |
| let results = rules.map(rule => { | |
| let validator = this.ruleValidators[rule.name] | |
| if (!validator.validate(val, rule.value, contextData)) { | |
| let realMessage = rule.message || validator.message | |
| return { | |
| name: rule.name, | |
| message: isFunction(realMessage) | |
| ? realMessage(val, rule.value) | |
| : (realMessage + '') | |
| .replace(ruleValueRe, rule.value) | |
| .replace(valueRe, val) | |
| } | |
| } | |
| // 代表没错 | |
| return true | |
| }) | |
| // 只返回出错的就好 | |
| results = results.filter(res => isObject(res)) | |
| return results.length ? results : true | |
| } | |
| initRules (rules) { | |
| // 根据优先级排一下显示顺序 | |
| rules.sort((x, y) => { | |
| let priorityX = x.priority || this.ruleValidators[x.name].priority | |
| let priorityY = y.priority || this.ruleValidators[y.name].priority | |
| return priorityX >= priorityY | |
| }) | |
| } | |
| addRule (rule, validator) { | |
| if (!(rule in this.ruleValidators)) { | |
| this.ruleValidators[rule] = validator | |
| } | |
| } | |
| } | |
| export default new Rule() | |