Sha256: a7e18c37d2b4453f46a1ca2a16230db17985a604889e9e40230a85a1fe4da258

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

window.SmsTools ?= {}

class SmsTools.Message
  maxLengthForEncoding:
    ascii:
      normal: 160
      concatenated: 153
    gsm:
      normal: 160
      concatenated: 153
    unicode:
      normal: 70
      concatenated: 67

  doubleByteCharsInGsmEncoding:
    '^':  true
    '{':  true
    '}':  true
    '[':  true
    '~':  true
    ']':  true
    '|':  true
    '€':  true
    '\\': true

  asciiPattern: /^[\x00-\x7F]*$/
  gsmEncodingPattern: /^[0-9a-zA-Z@Δ¡¿£_!Φ"¥Γ#èΛ¤éΩ%ùΠ&ìΨòΣçΘΞ:Ø;ÄäøÆ,<Ööæ=ÑñÅß>Üüåɧà€~ \$\.\-\+\(\)\*\\\/\?\|\^\}\{\[\]\'\r\n]*$/

  constructor: (@text) ->
    @text                   = @text.replace /\r\n?/g, "\n"
    @encoding               = @_encoding()
    @length                 = @_length()
    @concatenatedPartsCount = @_concatenatedPartsCount()

  maxLengthFor: (concatenatedPartsCount) ->
    messageType = if concatenatedPartsCount > 1 then 'concatenated' else 'normal'

    concatenatedPartsCount * @maxLengthForEncoding[@encoding][messageType]

  use_gsm_encoding: ->
    if SmsTools['use_gsm_encoding'] == undefined
      true
    else
      SmsTools['use_gsm_encoding']

  _encoding: ->
    if @asciiPattern.test(@text)
      'ascii'
    else if @use_gsm_encoding() and @gsmEncodingPattern.test(@text)
      'gsm'
    else
      'unicode'

  _concatenatedPartsCount: ->
    encoding = @encoding
    length   = @length

    if length <= @maxLengthForEncoding[encoding].normal
      1
    else
      parseInt Math.ceil(length / @maxLengthForEncoding[encoding].concatenated), 10

  # Returns the number of symbols which the given text will eat up in an SMS
  # message, taking into account any double-space symbols in the GSM 03.38
  # encoding.
  _length: ->
    length = @text.length

    if @encoding == 'gsm'
      for char in @text
        length += 1 if @doubleByteCharsInGsmEncoding[char]

    length

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
smstools-0.2.0 lib/assets/javascripts/sms_tools/message.js.coffee
smstools-0.1.1 lib/assets/javascripts/sms_tools/message.js.coffee
smstools-0.1.0 lib/assets/javascripts/sms_tools/message.js.coffee