Skip to content

Commit

Permalink
Merge pull request #3 from oklas/flattenization
Browse files Browse the repository at this point in the history
Working on flattenization
  • Loading branch information
oklas committed Aug 1, 2017
2 parents 424275c + 04bd7e6 commit 9d41b30
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ function flattenLowness(lowness, obj, opts) {
var res = {};
if(lowness) {
Object.keys(obj).forEach(function(key) {
res[key] = flattenLowness(lowness-1, obj[key], opts);
if(obj[key] instanceof Array || obj[key] instanceof Object) {
res[key] = flattenLowness(lowness-1, obj[key], opts);
} else {
res[key] = obj[key]
}
});
} else {
res = flatten(obj, opts);
Expand All @@ -28,7 +32,7 @@ function IntlPlugin(options) {
var lowness = options.flattenLowness;
lowness = undefined === lowness ? 1 : lowness;
var depth = options.flattenDepth;
var flattenOpts = depth ? {} : {maxDepth: depth};
var flattenOpts = depth ? {maxDepth: depth} : {};

options.save = function(common) {
return JSON.stringify(
Expand Down
76 changes: 76 additions & 0 deletions test/functions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var IntlPlugin = require("../");

var fs = require("fs");
var path = require("path");
var expect = require("expect");
var webpack = require("webpack");

var packageDir = path.join(__dirname, "package");
var distDir = path.join(__dirname, "dist");

describe('Test function join', function() {
it('is merge', function() {
var intl = new IntlPlugin({
search: 'none'
})

var res = intl.options.join({a:1}, '{"b":2}')
expect(res.a).toEqual(1)
expect(res.b).toEqual(2)
})
})

describe('Test function save', function() {
it('can do full flattenization', function() {
var intl = new IntlPlugin({
search: 'none',
flattenLowness: 0
})
var res = JSON.parse(intl.options.save(
{a: {b1: 'a.b1', b2: {c: 'a.b2.c'}}, aa:[{c:'aa.0.c'}]}
))
expect(res['a.b1']).toEqual('a.b1')
expect(res['a.b2.c']).toEqual('a.b2.c')
expect(res['aa.0.c']).toEqual('aa.0.c')
})

it('can do flattenization started from some lowness', function() {
var intl = new IntlPlugin({
search: 'none',
flattenLowness: 1,
})
var res = JSON.parse(intl.options.save(
{a: {b1: 'a.b1', b2: {c: 'a.b2.c'}}, aa:[{c:'aa.0.c'}]}
))
expect(res['a']['b1']).toEqual('a.b1')
expect(res['a']['b2.c']).toEqual('a.b2.c')
expect(res['aa']['0.c']).toEqual('aa.0.c')
})

it('can do flattenization through some depth', function() {
var intl = new IntlPlugin({
search: 'none',
flattenLowness: 0,
flattenDepth: 2
})
var res = JSON.parse(intl.options.save(
{a: {b1: 'a.b1', b2: {c: 'a.b2.c'}}, aa:[{c:'aa.0.c'}]}
))
expect(res['a.b1']).toEqual('a.b1')
expect(res['a.b2']['c']).toEqual('a.b2.c')
expect(res['aa.0']['c']).toEqual('aa.0.c')
})

it('flattenization must not split string as array', function() {
var intl = new IntlPlugin({
search: 'none',
flattenLowness: 2,
})
var res = JSON.parse(intl.options.save(
{a: {b1: 'a.b1', b2: {c: 'a.b2.c'}}, aa:[{c:'aa.0.c'}]}
))
expect(res['a']['b1']).toEqual('a.b1')
expect(res['a']['b2']['c']).toEqual('a.b2.c')
expect(res['aa']['0']['c']).toEqual('aa.0.c')
})
})

0 comments on commit 9d41b30

Please sign in to comment.