Skip to content

Commit 779008a

Browse files
author
Martin Thurau
committed
Initial version - converted from require-yaml
0 parents  commit 779008a

File tree

14 files changed

+326
-0
lines changed

14 files changed

+326
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.sublime-project
2+
*.sublime-workspace
3+
*.idea
4+
node_modules/
5+
bower_components/
6+
test/main-built.js

.jshintrc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"laxcomma" : true
3+
, "smarttabs" : true
4+
, "unused" : "vars"
5+
, "forin" : true
6+
, "quotmark" : "single"
7+
, "undef" : true
8+
, "browser" : true
9+
, "noempty" : true
10+
, "latedef" : true
11+
, "noarg" : true
12+
, "nonew" : true
13+
, "freeze" : true
14+
, "eqeqeq" : true
15+
, "immed" : true
16+
, "globals" : {
17+
"define" : true
18+
, "require" : true
19+
, "requirejs" : true
20+
, "console" : true
21+
, "module" : true
22+
}
23+
}

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
before_script:
2+
- npm install -g bower
3+
- echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
4+
- bower install -f
5+
language: node_js
6+
node_js:
7+
- "0.12"
8+
- "0.10"
9+
sudo: false

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#require-java-properties
2+
3+
[![Build Status](https://travis-ci.org/martinth/require-java-properties.svg?branch=master)](https://travis-ci.org/martinth/require-java-properties)
4+
5+
> a transform for loading Java .properties files in RequireJS, using [java.properties.js][1]
6+
7+
## Install the plugin and its dependencies using bower
8+
```sh
9+
$ bower install java.properties.js --save
10+
```
11+
If you prefer a manual install, make sure to satisfy the `java.properties.js` and `text` dependencies.
12+
13+
## Simple usage
14+
First, make sure the module names `java.properties.js` and `text` resolve to the proper locations:
15+
```js
16+
requirejs.config({
17+
paths : {
18+
'text': './bower_components/requirejs-text/text',
19+
'java.properties': './bower_components/java.properties.js/dist/amd/java.properties',
20+
'properties': './bower_components/require-java-properties/properties'
21+
}
22+
});
23+
```
24+
25+
Now you're ready to `require` Java properties files as plain JavaScript objects:
26+
```js
27+
require('properties!some/folder/afile.properties', function(data){
28+
console.log(data); // => logs the transformed object
29+
});
30+
```
31+
**NOTE**: In contrast to the default [java.properties.js][1] behaviour keys with dots in them will *not* result in nested
32+
objects. So a properties key like:
33+
```
34+
key=12
35+
key.with.dots=foobar
36+
```
37+
Will result in this JSON object:
38+
```js
39+
{
40+
"key": 12,
41+
"key.with.dots": "foobar"
42+
}
43+
```
44+
This is due to the specific requirements for which this plugin was build.
45+
46+
## Build time
47+
On build time you can simply use `stubModules` in your build config to get rid of the parser in the file you ship to production:
48+
```js
49+
({
50+
stubModules: ['text', 'java.properties', 'properties']
51+
})
52+
```
53+
54+
##License
55+
MIT
56+
57+
##Attribution
58+
This plugin is a based upon [require-yaml](https://github.com/m90/require-yaml).
59+
60+
[1]: https://github.com/mattdsteele/java-properties

bower.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "require-java-properties",
3+
"version": "0.1.0",
4+
"authors": [
5+
"Martin Thurau <martin.thurau@gmail.com>"
6+
],
7+
"description": "RequireJS loader for java properties files",
8+
"main": "properties.js",
9+
"keywords": [
10+
"requirejs",
11+
"loader",
12+
"plugin",
13+
"amd",
14+
"java",
15+
"properties"
16+
],
17+
"bugs": {
18+
"url": "https://github.com/martinth/require-java-properties"
19+
},
20+
"url": "https://github.com/martinth/require-java-properties",
21+
"license": "MIT",
22+
"ignore": [
23+
"**/.*",
24+
"node_modules",
25+
"bower_components",
26+
"test",
27+
"tests"
28+
],
29+
"repository": {
30+
"type": "git",
31+
"url": "git://github.com/martinth/require-java-properties"
32+
},
33+
"dependencies": {
34+
"requirejs-text": "~2.0.12",
35+
"java.properties.js": "~1.0.0"
36+
},
37+
"devDependencies": {
38+
"almond": "~0.3.0"
39+
}
40+
}

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "require-java-properties",
3+
"version": "0.1.0",
4+
"description": "RequireJS loader for java properties files",
5+
"url": "https://github.com/martinth/require-java-properties",
6+
"bugs": {
7+
"url": "https://github.com/martinth/require-java-properties"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git://github.com/martinth/require-java-properties"
12+
},
13+
"main": "properties.js",
14+
"scripts": {
15+
"pretest": "node node_modules/jshint/bin/jshint properties.js",
16+
"test": "node node_modules/mocha/bin/mocha test/test.js"
17+
},
18+
"keywords": [
19+
"requirejs",
20+
"lader",
21+
"plugin",
22+
"amd",
23+
"java",
24+
"properties"
25+
],
26+
"author": "Martin Thurau <martin.thurau@gmail.com>",
27+
"license": "MIT",
28+
"dependencies": {
29+
"java.properties.js": "~1.0.0"
30+
},
31+
"devDependencies": {
32+
"jshint": "^2.5.6",
33+
"mocha": "^2.0.1",
34+
"requirejs": "^2.1.15"
35+
}
36+
}

properties.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
define(['text', 'java.properties'], function (text, javaProperties) {
2+
var buildMap = {};
3+
4+
var flattenObject = function (ob) {
5+
return Object.keys(ob).reduce(function (toReturn, k) {
6+
if ((typeof ob[k]) === 'object' && ob[k]) {
7+
var flatObject = flattenObject(ob[k]);
8+
Object.keys(flatObject).forEach(function (k2) {
9+
toReturn[k + '.' + k2] = flatObject[k2];
10+
});
11+
}
12+
else {
13+
toReturn[k] = ob[k];
14+
}
15+
16+
return toReturn;
17+
}, {});
18+
};
19+
20+
return {
21+
write: function (pluginName, name, write) {
22+
if (name in buildMap) {
23+
write('define("' + pluginName + '!' + name + '", function(){ return ' + JSON.stringify(buildMap[name]) + '; });\n');
24+
}
25+
},
26+
load: function (name, parentRequire, onload, config) {
27+
text.get(parentRequire.toUrl(name), function (propertiesString) {
28+
try {
29+
var result = javaProperties.propertiesToObject(propertiesString);
30+
var flattened = flattenObject(result);
31+
buildMap[name] = flattened;
32+
onload(flattened);
33+
} catch (e){
34+
onload.error(e);
35+
}
36+
});
37+
38+
}
39+
, version: '0.1.0'
40+
};
41+
});

test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main-built.js

test/fixtures/invalid.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
this
2+
3+
is not
4+
valid:data

test/fixtures/test.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This is a comment
2+
! Also a comment
3+
short text
4+
pi=3.14
5+
lang:en
6+
longtext This text \
7+
is over multiple lines.
8+
paramText=Text with dynamic parameters: {0}
9+
unicodeText=a smiley: ☺
10+
deeply.nested.object=value

test/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Test</title>
6+
</head>
7+
<body>
8+
<script src="main-built.js"></script>
9+
</body>
10+
</html>

test/loadindex.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var page = require('webpage').create();
2+
page.open('test/index.html', function(status){
3+
setTimeout(function(){
4+
console.log(page.content);
5+
phantom.exit();
6+
}, 2000);
7+
});

test/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require(['properties!test/fixtures/test.properties'], function (data) {
2+
var headline = document.createElement('h1');
3+
headline.innerHTML = data['deeply.nested.object'];
4+
headline.id = 'fixture';
5+
document.body.appendChild(headline);
6+
});

test/test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var requirejs = require('requirejs');
2+
var assert = require('assert');
3+
4+
requirejs.config({
5+
nodeRequire: require,
6+
baseUrl: '.',
7+
paths: {
8+
'text': './bower_components/requirejs-text/text',
9+
'java.properties': './bower_components/java.properties.js/dist/amd/java.properties',
10+
'properties': './properties'
11+
}
12+
});
13+
14+
describe('properties loader', function () {
15+
16+
it('should render java properties into JavaScript objects', function (done) {
17+
requirejs(['properties!test/fixtures/test.properties'], function (data) {
18+
assert.deepEqual(data, {
19+
short: 'text',
20+
pi: 3.14,
21+
lang: 'en',
22+
longtext: 'This text is over multiple lines.',
23+
paramText: 'Text with dynamic parameters: {0}',
24+
unicodeText: 'a smiley: ☺',
25+
'deeply.nested.object': 'value'
26+
});
27+
done();
28+
});
29+
});
30+
31+
it('should call the errback on malformed input', function(done){
32+
requirejs(['properties!test/fixtures/invalid.properties'], function () {
33+
assert(false);
34+
done();
35+
}, function(err){
36+
assert(true);
37+
done();
38+
});
39+
});
40+
41+
});
42+
43+
describe('properties writer', function (done) {
44+
it('compiles java properties into dependency-less modules', function (done) {
45+
46+
this.timeout(7500);
47+
48+
requirejs.optimize({
49+
baseUrl: '.',
50+
name: './bower_components/almond/almond',
51+
paths: {
52+
'text': './bower_components/requirejs-text/text',
53+
'java.properties': './bower_components/java.properties.js/dist/amd/java.properties',
54+
'properties': './properties'
55+
},
56+
optimize: 'none',
57+
include: ['./test/main'],
58+
stubModules: ['text', 'java.properties', 'properties'],
59+
out: 'test/main-built.js'
60+
}, function () {
61+
var exec = require('child_process').exec;
62+
var process = exec('phantomjs test/loadindex.js', function (err, stdout) {
63+
if (err) {
64+
assert(false);
65+
} else {
66+
assert(stdout.indexOf('<h1 id="fixture">value</h1>') > -1);
67+
}
68+
done();
69+
});
70+
});
71+
72+
});
73+
});

0 commit comments

Comments
 (0)