Skip to content

Instantly share code, notes, and snippets.

@dimified
Created April 16, 2016 15:40
Show Gist options
  • Save dimified/27ae5edcc05d6be66ab2aae77be41ae1 to your computer and use it in GitHub Desktop.
Save dimified/27ae5edcc05d6be66ab2aae77be41ae1 to your computer and use it in GitHub Desktop.
Module: Kongcat (Lightweight framework for localStorage objects)
'use strict'
define [
'jquery'
'angular'
], ($, Angular) ->
###*
# Registry of module kongcat
# @namespace App.kongcat
# @memberof App
###
Angular.module 'App.kongcat', []
.factory 'kongcat', [ () ->
return {
###*
# Remove the key value pair or all items by calling proper function
# @function
# @memberof App.moduless.kongcat
# @param {string} key Identifier for localStorage
###
clear: (key) ->
if key
localStorage.removeItem(key)
else
localStorage.clear()
return
###*
# Erase the value of the key by setting empty string
# @function
# @memberof App.modules.kongcat
# @param {string} key Identifier for localStorage
# @return {string}
###
erase: (key) ->
localStorage[key] = ''
return localStorage[key]
###*
# Remove the entry in localstorage
# @function
# @memberof App.modules.kongcat
# @param {string} key Identifier for localStorage
###
remove: (key) ->
delete localStorage[key]
return
###*
# Update value inside localStorage by pointing to the right position
# depending on the type of object
# @function
# @memberof App.modules.kongcat
# @param {string} key Identifier for localStorage
# @param {string|number|array|object} val Value which should be written
# @param {number|string} pos Position which should be updated in object
# @return {string}
###
update: (key, val, pos) ->
if pos || pos == 0
elem = this.read(key)
if typeof elem == 'object'
elem[pos] = val
val = elem
else
throw new TypeError 'Only objects and arrays are supported to work with position'
return this.write(key, val)
###*
# Writes a new variable into localStorage or overwrites existing keys.
# Parameters are mandatory
# @function
# @memberof App.modules.kongcat
# @param {string} key Identifier for localStorage
# @param {string|number|array|object} val Value which should be written
# '-* Only numbers in array are allowed yet
# @return {string}
###
write: (key, val) ->
if !key && !val
throw new ReferenceError 'Missing key and value parameters'
if typeof val == 'string'
localStorage[key] = '"' + val + '"'
else if typeof val == 'object'
localStorage[key] = JSON.stringify(val)
else
localStorage[key] = val
return localStorage[key]
###*
# Pushes to existing array in localStorage or creates an initial array with value
# Parameters are mandatory
# @function
# @memberof App.modules.kongcat
# @param {string} key Identifier for localStorage
# @param {number} val Value which should be written
# * Array: Only numbers in array are allowed yet
# @return {string}
###
push: (key, val) ->
if typeof val != 'number'
throw new TypeError 'Only numbers are allowed to push to array'
# Key exists
if localStorage[key] != undefined
if Object.prototype.toString.call(this.read(key)) != '[object Array]'
throw new TypeError 'Only arrays are allowed to use this function'
# push to existing array
array = this.read(key)
array.push(val)
this.write(key, array)
# Create new array with value if it does not exist
else
this.write(key, [val])
return localStorage[key]
###*
# Reads the required value of the localStorage
# @function
# @memberof App.modules.kongcat
# @param {string} key Identifier for localStorage
# @return {string|number|array|object}
###
read: (key) ->
if key
if localStorage[key] == undefined
throw new ReferenceError key + ' not found'
return JSON.parse(localStorage[key])
else
return localStorage
###*
# Return an array of saved projects
# @function
# @memberof App.modules.kongcat
# @return {array}
###
projects: () ->
array = []
i = 0
while i < localStorage.length
key = localStorage.key(i)
# Testing key with regular expression indicates an existing project
if /\//.test(key)
array.push({
title: key.split('/')[1]
time: key.split('/')[0]
data: localStorage.getItem(key)
})
i++
array
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment