/* global localStorage */
var applist = {}

applist.findAll = function () {
  var appsData = localStorage.getItem('apps')
  var apps = appsData ? JSON.parse(appsData) : []
  return Promise.resolve(apps)
}

applist.add = function (app) {
  app.id = Math.random().toString(36).substr(2, 7)
  return applist.findAll()
    .then(function (apps) {
      apps.push(app)
      localStorage.setItem('apps', JSON.stringify(apps))
      return app
    })
}
applist.find = function (id) {
  return applist.findAll()
    .then(function (apps) {
      var findApp = function (app) {
        return app.id === id
      }
      return apps.find(findApp)
    })
}
applist.add = function (app) {
  app.id = Math.random().toString(36).substr(2, 7)
  return applist.findAll()
    .then(function (apps) {
      apps.push(app)
      localStorage.setItem('apps', JSON.stringify(apps))
      return app
    })
}
applist.update = function(app){
  return applist.findAll()
    .then(function (apps) {
      var newApps =apps.map(function (currentApp) {
        if (currentApp.id === app.id) {
          return {name:app.name, id:app.id}
        } else {
          return currentApp
        }
      })
      localStorage.setItem('apps', JSON.stringify(newApps))
      return app
    })
}

@@ -645,248 +645,8 @@
 )%0A%7D%0A
-applist.add = function (app) %7B%0A  app.id = Math.random().toString(36).substr(2, 7)%0A  return applist.findAll()%0A    .then(function (apps) %7B%0A      apps.push(app)%0A      localStorage.setItem('apps', JSON.stringify(apps))%0A      return app%0A    %7D)%0A%7D
 %0Aapp

delete the duplicated applist.add