https://github.com/fflorent/safe-nav
sweet.js macro for safe navigation à la CoffeeScript
https://github.com/fflorent/safe-nav
Last synced: 10 months ago
JSON representation
sweet.js macro for safe navigation à la CoffeeScript
- Host: GitHub
- URL: https://github.com/fflorent/safe-nav
- Owner: fflorent
- Created: 2015-01-02T19:05:10.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-01-11T12:59:53.000Z (over 11 years ago)
- Last Synced: 2025-01-15T10:02:21.458Z (over 1 year ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Safe-nav
This is an experimental [sweet.js](http://sweetjs.org) macro
that implements [existential accessor operator](http://coffeescript.org/#try:a%3F.b()%3F.c%0A%0Aif%20a%3F.b()%3F.c%0A%20%20d()) (aka "?.") of CoffeeScript
or the [Safe-Navigation](http://docs.codehaus.org/display/GROOVY/Operators#Operators-SafeNavigationOperator%28%3F.%29) operator of Groovy.
So you can easily change a piece of code of this type:
````js
if (ancestor) {
var child = ancestor.querySelector(".class");
if (child)
child.remove();
}
````
to
````js
ancestor?.querySelector(".class")?.remove();
````
Or:
````js
if (console && console.dirxml)
console.dirxml(document.body);
````
to:
````js
console?.dirxml?(document.body);
````
More concise, isn't it?
## Installing
````bash
$ npm install sweet.js safe-nav
````
## Using
````bash
$ sjs -m safe-nav -o compiled.js my_sweet_code.js
````
If you pass -c to sjs along with -o output.js, it will generate a sourcemap so you get good debugging too!
## Licence
Copyright © 2014 Florent Fayolle
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
## TODO
* Tests
* Optimizations