simple-save
Advanced tools
Comparing version 1.0.0 to 1.0.1
14
index.js
var fs = require("fs"); | ||
delete require.cache[__filename]; | ||
var escapeRegex = function(s) { | ||
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
}; | ||
module.exports = function (pattern) { | ||
var PATTERN = pattern || "SS"; | ||
return function(name, value) { | ||
return function(name, value, suffix) { | ||
//console.log("Changing "+name+" to `" + value + "`"); | ||
name = escapeRegex(name); | ||
suffix = escapeRegex(suffix); | ||
fs.readFile(module.parent.filename, "utf-8", function(err, data) { | ||
if(err) throw err; | ||
var match = "(\\/\\*"+PATTERN+"\\*\\/.*?"+name+".*?=).*;"; | ||
var match = "(\\/\\*" + PATTERN + | ||
(suffix ? ":" + suffix : "") + | ||
"\\*\\/.*?" + name + ".*?=).*;"; | ||
var replace = '$1 '+JSON.stringify(value)+";"; | ||
@@ -20,1 +27,2 @@ data = data.replace(RegExp(match, "gm"), replace); | ||
}; | ||
{ | ||
"name": "simple-save", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A stupidly simple way to persist data.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,3 +11,12 @@ # simple-save | ||
# Usage | ||
```js | ||
save(variableName:string, newValue, [suffix:string]) | ||
``` | ||
Call the package with the name of the variable you want to persist, and the new | ||
value. This will replace the assignments of all tagged occurrences of this | ||
variable with the new value. | ||
```js | ||
var save = require("simple-save")(); | ||
@@ -20,2 +29,5 @@ /*SS*/ var test = 3; | ||
# Advanced | ||
If you want to set a custom tag instead of the default of `"SS"`, pass in a | ||
@@ -26,1 +38,15 @@ string: | ||
``` | ||
You can also set custom tag suffixes on a per-variable basis as follows. In the | ||
following example, only the value of the variable within the function is modified. | ||
```js | ||
var save = require("simple-save")(); | ||
/*SS*/ var test = 3; | ||
function ttt(){ | ||
/*SS:1*/ var test = 3; | ||
} | ||
test = 5 | ||
save("test", test, "1"); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3005
24
50