Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/request / src / deps / toStringy.ts
Size: Mime:
import { isStringPrimitive, isArray, isObj, isJSON } from 'exotic'

const store = new WeakMap()
function wipeUndefined(x) {
  if (isArray(x)) {
    return x.map(wipeUndefined)
  } else if (isObj(x)) {
    if (store.has(x)) {
      return x
    }
    store.set(x, true)

    Object.keys(x).forEach(key => {
      if (x[key] === undefined || x[key] === null) {
        // delete x[key]
        x[key] = ''
      } else if (isStringPrimitive(x[key])) {
        // to stringify +1?
        if (isJSON(x[key])) {
          x[key] = JSON.parse(x[key])
        }
      }
    })
    return x
  } else {
    return x
  }
}

export function toStringy(x: string | any): string {
  if (x === null) {
    return ''
  } else if (x === undefined) {
    return ''
  } else if (isStringPrimitive(x)) {
    return x.replace(/\\"/g, '"')
  } else {
    return JSON.stringify(wipeUndefined(x)).replace(/\\"/g, '"')
  }
}