# stringToArray

# Description

string 转换为 数组

# Params

{String} string

# Return

Array

# Depend

import asciiToArray from './asciiToArray.js'
import hasUnicode from './hasUnicode.js'
import unicodeToArray from './unicodeToArray.js'

asciiToArray 源码分析

hasUnicode 源码分析

unicodeToArray 源码分析

# Code

function stringToArray(string) {
  return hasUnicode(string)
    ? unicodeToArray(string)
    : asciiToArray(string)
}

# Analyze

判断 string 是否含有 unicode 码,如果有调用 unicodeToArray 否则调用 asciiToArraystring.split

# Remark

  1. 三元运算符 MDN (opens new window)

# Example

stringToArray('1,2,3,4') // asciiToArray ['1', '2', '3', '4']
stringToArray('笑脸😊') // unicodeToArray [ '笑',  '脸',  '😊']