Skip to content

Commit

Permalink
Add useSpaceAsSeparator option to MPEdnWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
scramjet committed Feb 29, 2020
1 parent f15f5dd commit 8e22d5f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
8 changes: 7 additions & 1 deletion MPEdn/MPEdnWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#import <Foundation/Foundation.h>

//#import "MPEdnTaggedValueWriter.h"
@protocol MPEdnTaggedValueWriter;

/**
Expand Down Expand Up @@ -82,6 +81,13 @@ BOOL MPEdnIsCharacter (NSNumber *number);
*/
@property (readwrite) BOOL useKeywordsInMaps;

/**
* When true (default false), use a single space rather than a comma as the
* separator between items in lists, vectors, and sets, and between pairs of
* items in maps.
*/
@property (readwrite) BOOL useSpaceAsSeparator;

/**
* Take a Cocoa object, and generate an EDN-formatted string.
*
Expand Down
20 changes: 16 additions & 4 deletions MPEdn/MPEdnWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ @implementation MPEdnWriter
{
NSMutableString *outputStr;
BOOL useKeywordsInMaps;
NSString *separator;
NSArray *writers;
}

Expand Down Expand Up @@ -89,6 +90,7 @@ - (id) init
{
useKeywordsInMaps = NO;
writers = copy (defaultWriters);
separator = @",";
}

return self;
Expand All @@ -104,6 +106,16 @@ - (void) setUseKeywordsInMaps: (BOOL) newValue
useKeywordsInMaps = newValue;
}

- (BOOL) useSpaceAsSeparator
{
return [separator isEqualToString: @" "];
}

- (void) setUseSpaceAsSeparator: (BOOL) newValue
{
separator = newValue ? @" " : @",";
}

- (void) addTagWriter: (id<MPEdnTaggedValueWriter>) writer
{
NSMutableArray *newWriters = [writers mutableCopy];
Expand Down Expand Up @@ -293,7 +305,7 @@ - (void) outputDictionary: (NSDictionary *) value
for (id key in value)
{
if (!firstItem)
[outputStr appendString: @","];
[outputStr appendString: separator];

if (useKeywordsInMaps && [key isKindOfClass: [NSString class]])
{
Expand Down Expand Up @@ -323,8 +335,8 @@ - (void) outputArray: (NSArray *) value
for (id item in value)
{
if (!firstItem)
[outputStr appendString: @","];
[outputStr appendString: separator];

[self outputObject: item];

firstItem = NO;
Expand All @@ -342,7 +354,7 @@ - (void) outputSet: (NSSet *) value
for (id item in value)
{
if (!firstItem)
[outputStr appendString: @","];
[outputStr appendString: separator];

[self outputObject: item];

Expand Down
10 changes: 10 additions & 0 deletions MPEdnTests/MPEdnWriterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ - (void) testLists
NSArray *list = @[@"hello", @1];
MPAssertSerialisesOK (list, @"[\"hello\",1]");
}

// test useSpaceAsSeparator
{
MPEdnWriter *writer = [MPEdnWriter new];
writer.useSpaceAsSeparator = YES;

NSString *str = [writer serialiseToEdn: @[@1, @2]];

XCTAssertEqualObjects (str, @"[1 2]", @"Serialise");
}
}

- (void) testSets
Expand Down

0 comments on commit 8e22d5f

Please sign in to comment.