(data = {})
| 158 | * @returns {Object} data - Validated data. |
| 159 | */ |
| 160 | const validate = function(data = {}) { |
| 161 | |
| 162 | // Copy root object to avoid changes by reference |
| 163 | data = Object.assign({}, data) |
| 164 | |
| 165 | if (data.inside == null) data.inside = () => {} |
| 166 | if (data.outside == null) data.outside = () => {} |
| 167 | if (data.direct == null) data.direct = false |
| 168 | if (data.track == null) data.track = true |
| 169 | if (data.props == null) data.props = {} |
| 170 | |
| 171 | if (data.from == null) throw new Error('Missing property `from`') |
| 172 | if (data.to == null) throw new Error('Missing property `to`') |
| 173 | if (typeof data.inside !== 'function') throw new Error('Property `inside` must be undefined or a function') |
| 174 | if (typeof data.outside !== 'function') throw new Error('Property `outside` must be undefined or a function') |
| 175 | if (typeof data.direct !== 'boolean' && data.direct instanceof HTMLElement === false) throw new Error('Property `direct` must be undefined, a boolean or a DOM element/node') |
| 176 | if (data.direct === true && data.elem == null) throw new Error('Property `elem` is required when `direct` is true') |
| 177 | if (typeof data.track !== 'boolean') throw new Error('Property `track` must be undefined or a boolean') |
| 178 | if (typeof data.props !== 'object') throw new Error('Property `props` must be undefined or an object') |
| 179 | |
| 180 | if (data.elem == null) { |
| 181 | |
| 182 | if (isAbsoluteValue(data.from) === false) throw new Error('Property `from` must be a absolute value when no `elem` has been provided') |
| 183 | if (isAbsoluteValue(data.to) === false) throw new Error('Property `to` must be a absolute value when no `elem` has been provided') |
| 184 | |
| 185 | } else { |
| 186 | |
| 187 | if (isRelativeValue(data.from) === true) data.from = relativeToAbsoluteValue(data.from, data.elem) |
| 188 | if (isRelativeValue(data.to) === true) data.to = relativeToAbsoluteValue(data.to, data.elem) |
| 189 | |
| 190 | } |
| 191 | |
| 192 | data.from = parseAbsoluteValue(data.from) |
| 193 | data.to = parseAbsoluteValue(data.to) |
| 194 | |
| 195 | // Create a new props object to avoid changes by reference |
| 196 | data.props = Object.keys(data.props).reduce((acc, key) => { |
| 197 | |
| 198 | // Copy prop object to avoid changes by reference |
| 199 | const prop = Object.assign({}, data.props[key]) |
| 200 | |
| 201 | if (isAbsoluteValue(prop.from) === false) throw new Error('Property `from` of prop must be a absolute value') |
| 202 | if (isAbsoluteValue(prop.to) === false) throw new Error('Property `from` of prop must be a absolute value') |
| 203 | |
| 204 | prop.from = parseAbsoluteValue(prop.from) |
| 205 | prop.to = parseAbsoluteValue(prop.to) |
| 206 | |
| 207 | if (prop.timing == null) prop.timing = eases['linear'] |
| 208 | |
| 209 | if (typeof prop.timing !== 'string' && typeof prop.timing !== 'function') throw new Error('Property `timing` of prop must be undefined, a string or a function') |
| 210 | |
| 211 | if (typeof prop.timing === 'string' && eases[prop.timing] == null) throw new Error('Unknown timing for property `timing` of prop') |
| 212 | if (typeof prop.timing === 'string') prop.timing = eases[prop.timing] |
| 213 | |
| 214 | acc[key] = prop |
| 215 | |
| 216 | return acc |
| 217 |
no test coverage detected