Skip to content

Commit

Permalink
Merge pull request #68 from thomscode/develop
Browse files Browse the repository at this point in the history
Fix issue 59
  • Loading branch information
jeremyfa committed Feb 11, 2017
2 parents 62c012c + c3ed21f commit d6e9f2f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 16 deletions.
20 changes: 17 additions & 3 deletions dist/yaml.debug.js

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions dist/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,8 @@ module.exports = Unescaper;


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

Pattern = require('./Pattern');

Expand Down Expand Up @@ -1534,7 +1535,20 @@ Utils = (function() {
};

Utils.isEmpty = function(value) {
return !value || value === '' || value === '0' || (value instanceof Array && value.length === 0);
return !value || value === '' || value === '0' || (value instanceof Array && value.length === 0) || this.isEmptyObject(value);
};

Utils.isEmptyObject = function(value) {
var k;
return value instanceof Object && ((function() {
var results;
results = [];
for (k in value) {
if (!hasProp.call(value, k)) continue;
results.push(k);
}
return results;
})()).length === 0;
};

Utils.subStrCount = function(string, subString, start, length) {
Expand Down
2 changes: 1 addition & 1 deletion dist/yaml.min.js

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions lib/Utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions src/Utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,23 @@ class Utils
return str.replace(regexRight, '')


# Checks if the given value is empty (null, undefined, empty string, string '0')
# Checks if the given value is empty (null, undefined, empty string, string '0', empty Array, empty Object)
#
# @param [Object] value The value to check
#
# @return [Boolean] true if the value is empty
#
@isEmpty: (value) ->
return not(value) or value is '' or value is '0' or (value instanceof Array and value.length is 0)
return not(value) or value is '' or value is '0' or (value instanceof Array and value.length is 0) or @isEmptyObject(value)

# Checks if the given value is an empty object
#
# @param [Object] value The value to check
#
# @return [Boolean] true if the value is empty and is an object
#
@isEmptyObject: (value) ->
return value instanceof Object and (k for own k of value).length is 0

# Counts the number of occurences of subString inside string
#
Expand All @@ -100,22 +108,22 @@ class Utils
#
@subStrCount: (string, subString, start, length) ->
c = 0

string = '' + string
subString = '' + subString

if start?
string = string[start..]
if length?
string = string[0...length]

len = string.length
sublen = subString.length
for i in [0...len]
if subString is string[i...sublen]
c++
i += sublen - 1

return c


Expand Down Expand Up @@ -308,7 +316,7 @@ class Utils
callback(null)
xhr.open 'GET', path, true
xhr.send null

else
# Sync
xhr.open 'GET', path, false
Expand Down
3 changes: 3 additions & 0 deletions test/spec/YamlSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,10 @@ describe 'Dumped YAML Inline Collections', ->
expect YAML.parse(YAML.dump({key:[]}))
.toEqual({key:[]})

it 'can be dumpted empty inline collections', ->

expect YAML.parse(YAML.dump({key:{}}))
.toEqual({key:{}})

describe 'Dumped YAML Basic Types', ->

Expand Down
9 changes: 8 additions & 1 deletion test/spec/YamlSpec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d6e9f2f

Please sign in to comment.