Skip to content

Commit d6e9f2f

Browse files
authored
Merge pull request #68 from thomscode/develop
Fix issue 59
2 parents 62c012c + c3ed21f commit d6e9f2f

File tree

7 files changed

+76
-16
lines changed

7 files changed

+76
-16
lines changed

dist/yaml.debug.js

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/yaml.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,8 @@ module.exports = Unescaper;
14651465

14661466

14671467
},{"./Pattern":7,"./Utils":9}],9:[function(require,module,exports){
1468-
var Pattern, Utils;
1468+
var Pattern, Utils,
1469+
hasProp = {}.hasOwnProperty;
14691470

14701471
Pattern = require('./Pattern');
14711472

@@ -1534,7 +1535,20 @@ Utils = (function() {
15341535
};
15351536

15361537
Utils.isEmpty = function(value) {
1537-
return !value || value === '' || value === '0' || (value instanceof Array && value.length === 0);
1538+
return !value || value === '' || value === '0' || (value instanceof Array && value.length === 0) || this.isEmptyObject(value);
1539+
};
1540+
1541+
Utils.isEmptyObject = function(value) {
1542+
var k;
1543+
return value instanceof Object && ((function() {
1544+
var results;
1545+
results = [];
1546+
for (k in value) {
1547+
if (!hasProp.call(value, k)) continue;
1548+
results.push(k);
1549+
}
1550+
return results;
1551+
})()).length === 0;
15381552
};
15391553

15401554
Utils.subStrCount = function(string, subString, start, length) {

dist/yaml.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Utils.js

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Utils.coffee

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,23 @@ class Utils
7979
return str.replace(regexRight, '')
8080

8181

82-
# Checks if the given value is empty (null, undefined, empty string, string '0')
82+
# Checks if the given value is empty (null, undefined, empty string, string '0', empty Array, empty Object)
8383
#
8484
# @param [Object] value The value to check
8585
#
8686
# @return [Boolean] true if the value is empty
8787
#
8888
@isEmpty: (value) ->
89-
return not(value) or value is '' or value is '0' or (value instanceof Array and value.length is 0)
89+
return not(value) or value is '' or value is '0' or (value instanceof Array and value.length is 0) or @isEmptyObject(value)
9090

91+
# Checks if the given value is an empty object
92+
#
93+
# @param [Object] value The value to check
94+
#
95+
# @return [Boolean] true if the value is empty and is an object
96+
#
97+
@isEmptyObject: (value) ->
98+
return value instanceof Object and (k for own k of value).length is 0
9199

92100
# Counts the number of occurences of subString inside string
93101
#
@@ -100,22 +108,22 @@ class Utils
100108
#
101109
@subStrCount: (string, subString, start, length) ->
102110
c = 0
103-
111+
104112
string = '' + string
105113
subString = '' + subString
106-
114+
107115
if start?
108116
string = string[start..]
109117
if length?
110118
string = string[0...length]
111-
119+
112120
len = string.length
113121
sublen = subString.length
114122
for i in [0...len]
115123
if subString is string[i...sublen]
116124
c++
117125
i += sublen - 1
118-
126+
119127
return c
120128

121129

@@ -308,7 +316,7 @@ class Utils
308316
callback(null)
309317
xhr.open 'GET', path, true
310318
xhr.send null
311-
319+
312320
else
313321
# Sync
314322
xhr.open 'GET', path, false

test/spec/YamlSpec.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,10 @@ describe 'Dumped YAML Inline Collections', ->
934934
expect YAML.parse(YAML.dump({key:[]}))
935935
.toEqual({key:[]})
936936

937+
it 'can be dumpted empty inline collections', ->
937938

939+
expect YAML.parse(YAML.dump({key:{}}))
940+
.toEqual({key:{}})
938941

939942
describe 'Dumped YAML Basic Types', ->
940943

test/spec/YamlSpec.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)