Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including cut-head with all npm packages installed. Try it out:

var cutHead = require("cut-head")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

cut-head v1.0.0

split a string into two part

cutHead = (str, delimeter)-> 
  unless str
    return [undefined, undefined]
  index = str.indexOf delimeter 
  if index > 0
    head = str.substr(0, index) 
    others = str.substr(index + 1)
    others = undefined if others.length is 0 
    return [head, others]
  return [str, undefined]

cutHead = require '../src'

describe 'cutHead', ()->
  it 'head#others', (done)->
    [head, others] = cutHead 'head#others', '#'  
 
    expect head
      .toEqual 'head'
    expect others
      .toEqual 'others' 
    done()
 
  it 'head', (done)->
    [head, others] = cutHead 'head', '#'  
 
    expect head
      .toEqual 'head'
    expect others
      .toEqual undefined 
    done()  
 
  it 'head#', (done)->
    [head, others] = cutHead 'head#', '#'  
 
    expect head
      .toEqual 'head'
    expect others
      .toEqual undefined 
    done()

 
  it 'null', (done)->
    [head, others] = cutHead null, '#'  
 
    expect head
      .toEqual undefined
    expect others
      .toEqual undefined 
    done() 
 
  it 'undefined', (done)->
    [head, others] = cutHead undefined, '#'  
 
    expect head
      .toEqual undefined
    expect others
      .toEqual undefined 
    done()

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free