Skip to content

Commit

Permalink
enhancement: do not split if types is empty list
Browse files Browse the repository at this point in the history
This is a minor change... if the `types` option is set to an empty
string or array, text will not be split.
  • Loading branch information
lukePeavey committed May 1, 2022
1 parent aefa002 commit 953c2c9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lib/SplitType.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export default class SplitType {
// revert them back to their original content before splitting them.
this.revert()

// If the `types` option is set to an empty array, text will not be split.
// @example new SplitType('#target', { types: [] })
if (!this.settings.types.length) {
return
}

// Create arrays to hold the split lines, words, and characters
this.lines = []
this.words = []
Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
lineClass: 'line',
wordClass: 'word',
charClass: 'char',
types: 'lines, words, chars',
types: ['lines', 'words', 'chars'],
absolute: false,
tagName: 'div',
}
3 changes: 3 additions & 0 deletions lib/utils/isArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isArray(value) {
return Array.isArray(value)
}
26 changes: 21 additions & 5 deletions lib/utils/parseSettings.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import extend from './extend'
import isString from './isString'
import isArray from './isArray'

/**
* Parses user supplied settings objects.
*/
export default function parseSettings(settings) {
export default function parseSettings(settings = {}) {
const object = extend(settings)
if (object.types || object.split) {
// Support `split` as an alias for `types`
object.types = object.types || object.split
// `split` may be used as an alias for the `types` option
// Parse the `types` settings into an array of valid split types.
// If `types` is explicitly set to an empty string or array, text will not be
// split at all.
let types
if (object.types !== undefined) {
types = object.types
} else if (object.split !== undefined) {
types = object.split
}

if (types !== undefined) {
object.types = (isString(types) || isArray(types) ? String(types) : '')
.split(',')
.map((type) => String(type).trim())
.filter((type) => /((line)|(word)|(char))/i.test(type))
}

// Support `position: absolute` as an alias for `absolute: true`
if (object.absolute || object.position) {
// Support `position: absolute` as alias for `absolute: true`
object.absolute = object.absolute || /absolute/.test(settings.position)
}
return object
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/parseTypes.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import isString from './isString'
import isArray from './isArray'
/**
* Takes a comma separated list of `types` and returns an objet
* Takes a list of `types` and returns an object
*
* @param {string | string[]} value a comma separated list of split types
* @return {{lines: boolean, words: boolean, chars: boolean}}
*/
export default function parseTypes(value) {
const types = isString(value) || Array.isArray(value) ? String(value) : ''
const types = isString(value) || isArray(value) ? String(value) : ''
return {
lines: /line/i.test(types),
words: /word/i.test(types),
chars: /(char)|(character)/i.test(types),
chars: /char/i.test(types),
}
}
3 changes: 2 additions & 1 deletion lib/utils/toArray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import isArrayLike from './isArrayLike'
import isArray from './isArray'

/**
* Coerces `value` to an `Array`.
Expand Down Expand Up @@ -27,7 +28,7 @@ import isArrayLike from './isArrayLike'
*
*/
export default function toArray(value) {
if (Array.isArray(value)) return value
if (isArray(value)) return value
if (value == null) return []
return isArrayLike(value) ? Array.prototype.slice.call(value) : [value]
}

0 comments on commit 953c2c9

Please sign in to comment.