Skip to content

Commit

Permalink
Supports dot-property and associative array syntax (closes #44)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmaccherone committed Nov 18, 2019
1 parent 5c4b330 commit fbc933c
Show file tree
Hide file tree
Showing 7 changed files with 1,581 additions and 115 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.DS_Store
scratch
coverage
.nyc_output
.play
node_modules
.idea
*.js
LocalStorage.js
*.map
18 changes: 0 additions & 18 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@ runSync = (command) ->
console.log("Output of running '#{command}'\n#{stdout}\n")
return stdout

task('clean', 'Deletes .js and .map files', () ->
folders = ['.']
for folder in folders
pathToClean = path.join(__dirname, folder)
contents = fs.readdirSync(pathToClean)
for file in contents when (_.endsWith(file, '.js') or _.endsWith(file, '.map'))
fs.unlinkSync(path.join(pathToClean, file))
)

task('compile', 'Compile CoffeeScript source files to JavaScript', () ->
process.chdir(__dirname)
fs.readdir('./', (err, contents) ->
files = ("#{file}" for file in contents when (file.indexOf('.coffee') > 0))
command = ['coffee', '-c'].concat(files).join(' ')
runSync(command)
)
)

task('publish', 'Publish to npm and add git tags', () ->
process.chdir(__dirname)
runSync('npm test') # Doing this externally to make it synchronous
Expand Down
37 changes: 18 additions & 19 deletions LocalStorage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,27 @@ class LocalStorage extends events.EventEmitter
@_init()
@_QUOTA_EXCEEDED_ERR = QUOTA_EXCEEDED_ERR

# if Proxy?
# handler =
# set: (receiver, key, value) =>
# if @[key]?
# return @[key] = value
# else
# @setItem(key, value)
#
# get: (receiver, key) =>
# if @[key]?
# return @[key]
# else
# return @getItem(key)
#
# instanceMap[@_location] = Proxy.create(handler, this)
# return instanceMap[@_location]
if Proxy?
handler =
set: (receiver, key, value) =>
if @[key]?
return @[key] = value
else
@setItem(key, value)

get: (receiver, key) =>
if @[key]?
return @[key]
else
return @getItem(key)

instanceMap[@_location] = new Proxy(this, handler)
return instanceMap[@_location]

# else it'll return this
instanceMap[@_location] = this
return instanceMap[@_location]

# else it'll return this


_init: () ->
try
stat = fs.statSync(@_location)
Expand Down
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@ _A drop-in substitute for the browser native localStorage API that runs on node.
* clear()
* Serializes to disk in the location specified during instantiation
* Supports the setting of a quota (default 5MB)
* Events. This doesn't exactly follow the spec which states that events are NOT supposed to be emitted to the browser window
that took the action that triggered the event in the first place. They are only to be emitted to listeners in
other browser windows. Early browser implementations actually did it this way and we don't really have the equivalent
of a browser window in node.js, so we choose to implement them in the current process. We did, however, include the pid
information in place of the window uri, so if we later wanted to say think of other node.js processes accessing
the same file system, we could actually implement it correctly. That would involve file watchers though and was more
than we wanted to implement for now. Maybe later.
* (temporarily removed) Associative array `localStorage['myKey'] = 'myValue'` and dot property `localStorage.myKey = 'myValue'`
syntax. This only works if you use the --harmony-proxies flag. It senses the existence of the Proxy object in the root
scope. If you have added your own Proxy object, then you could have a problem. Another potential risk is around
the "private", underscore preceded methods and properties. I had to reserve those in addition to the standard ones,
so you won't be able to use keys that overlap with those underscore preceded properties and methods in a harmony-proxies
environment.
* Events. This doesn't exactly follow the spec which states that events are NOT supposed to be emitted to the
browser window that took the action that triggered the event in the first place. They are only to be emitted to listeners in other browser windows. Early browser implementations actually did it this way and we don't really have the equivalent of a browser window in node.js, so we choose to implement them in the current process.
* Associative array `localStorage['myKey'] = 'myValue'` and dot property `localStorage.myKey = 'myValue'`
syntax. If you are in an ES6 supported environment. Limitations:
* You won't be able to use keys that collide with my "private" (starts with "_" like "_init") properties and
methods.

## Credits ##

Expand Down Expand Up @@ -88,7 +81,8 @@ node -r node-localstorage/register my-code.js

## Changelog ##

* 2.0.0 - 2019-10-17 - Updated all the depdendencies, added ability to register as polyfill (thanks @dy)
* 2.1.0 - 2019-11-17 - Added back dot-property and associative-array syntax using ES6 Proxy
* 2.0.0 - 2019-11-17 - Updated all the depdendencies, added ability to register as polyfill (thanks @dy)
* 1.3.1 - 2018-03-19 - Resolves issue #32 (thanks, plamens)
* 1.3.0 - 2016-04-09 - **Possibly backward breaking if you were using experimental syntax** Reverted experimental
associative array and dot-property syntax. The API for Proxy changed with node.js v6.x which broke it. Then when
Expand Down
Loading

0 comments on commit fbc933c

Please sign in to comment.