Sfoglia il codice sorgente

Merge pull request #416 from herby/jsobjproxy-at-ifpresent

JSObjectProxy >> at:IfPresent:{,ifAbsent:}
Nicolas Petton 11 anni fa
parent
commit
72940588c3

+ 30 - 0
js/Kernel-Objects.deploy.js

@@ -1482,6 +1482,36 @@ return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{aSymbol:aSymbol,a
 messageSends: []}),
 smalltalk.JSObjectProxy);
 
+smalltalk.addMethod(
+"_at_ifPresent_",
+smalltalk.method({
+selector: "at:ifPresent:",
+fn: function (aSymbol,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+		var obj = self['@jsObject'],
+			symbol = aSymbol._asString();
+		return symbol in obj ? aBlock(obj[symbol]) : nil;
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:",{aSymbol:aSymbol,aBlock:aBlock},smalltalk.JSObjectProxy)})},
+messageSends: []}),
+smalltalk.JSObjectProxy);
+
+smalltalk.addMethod(
+"_at_ifPresent_ifAbsent_",
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+fn: function (aSymbol,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+		var obj = self['@jsObject'],
+			symbol = aSymbol._asString();
+		return symbol in obj ? aBlock(obj[symbol]) : anotherBlock();
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{aSymbol:aSymbol,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.JSObjectProxy)})},
+messageSends: []}),
+smalltalk.JSObjectProxy);
+
 smalltalk.addMethod(
 "_at_put_",
 smalltalk.method({

+ 40 - 0
js/Kernel-Objects.js

@@ -2076,6 +2076,46 @@ referencedClasses: []
 }),
 smalltalk.JSObjectProxy);
 
+smalltalk.addMethod(
+"_at_ifPresent_",
+smalltalk.method({
+selector: "at:ifPresent:",
+category: 'accessing',
+fn: function (aSymbol,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+		var obj = self['@jsObject'],
+			symbol = aSymbol._asString();
+		return symbol in obj ? aBlock(obj[symbol]) : nil;
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:",{aSymbol:aSymbol,aBlock:aBlock},smalltalk.JSObjectProxy)})},
+args: ["aSymbol", "aBlock"],
+source: "at: aSymbol ifPresent: aBlock\x0a\x09\x22return the evaluation of aBlock with the value if the property is defined or return nil\x22\x0a\x09<\x0a\x09\x09var obj = self['@jsObject'],\x0a\x09\x09\x09symbol = aSymbol._asString();\x0a\x09\x09return symbol in obj ? aBlock(obj[symbol]) : nil;\x0a\x09>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxy);
+
+smalltalk.addMethod(
+"_at_ifPresent_ifAbsent_",
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+category: 'accessing',
+fn: function (aSymbol,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+		var obj = self['@jsObject'],
+			symbol = aSymbol._asString();
+		return symbol in obj ? aBlock(obj[symbol]) : anotherBlock();
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{aSymbol:aSymbol,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.JSObjectProxy)})},
+args: ["aSymbol", "aBlock", "anotherBlock"],
+source: "at: aSymbol ifPresent: aBlock ifAbsent: anotherBlock\x0a\x09\x22return the evaluation of aBlock with the value if the property is defined\x0a\x09or return value of anotherBlock\x22\x0a\x09<\x0a\x09\x09var obj = self['@jsObject'],\x0a\x09\x09\x09symbol = aSymbol._asString();\x0a\x09\x09return symbol in obj ? aBlock(obj[symbol]) : anotherBlock();\x0a\x09>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxy);
+
 smalltalk.addMethod(
 "_at_put_",
 smalltalk.method({

+ 56 - 0
js/Kernel-Tests.deploy.js

@@ -1884,6 +1884,62 @@ return self}, function($ctx1) {$ctx1.fill(self,"testAtIfAbsent",{testObject:test
 messageSends: ["jsObject", "assert:equals:", "at:ifAbsent:"]}),
 smalltalk.JSObjectProxyTest);
 
+smalltalk.addMethod(
+"_testAtIfPresent",
+smalltalk.method({
+selector: "testAtIfPresent",
+fn: function (){
+var self=this;
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("abc",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),nil);
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("e",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),"hello nil");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("a",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),"hello 1");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("f",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),"hello nil");
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfPresent",{testObject:testObject},smalltalk.JSObjectProxyTest)})},
+messageSends: ["jsObject", "assert:equals:", "at:ifPresent:", ",", "asString"]}),
+smalltalk.JSObjectProxyTest);
+
+smalltalk.addMethod(
+"_testAtIfPresentIfAbsent",
+smalltalk.method({
+selector: "testAtIfPresentIfAbsent",
+fn: function (){
+var self=this;
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("abc",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"not present");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("e",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"hello nil");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("a",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"hello 1");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("f",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"hello nil");
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfPresentIfAbsent",{testObject:testObject},smalltalk.JSObjectProxyTest)})},
+messageSends: ["jsObject", "assert:equals:", "at:ifPresent:ifAbsent:", ",", "asString"]}),
+smalltalk.JSObjectProxyTest);
+
 smalltalk.addMethod(
 "_testDNU",
 smalltalk.method({

+ 66 - 0
js/Kernel-Tests.js

@@ -2419,6 +2419,72 @@ referencedClasses: []
 }),
 smalltalk.JSObjectProxyTest);
 
+smalltalk.addMethod(
+"_testAtIfPresent",
+smalltalk.method({
+selector: "testAtIfPresent",
+category: 'tests',
+fn: function (){
+var self=this;
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("abc",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),nil);
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("e",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),"hello nil");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("a",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),"hello 1");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_("f",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})),"hello nil");
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfPresent",{testObject:testObject},smalltalk.JSObjectProxyTest)})},
+args: [],
+source: "testAtIfPresent\x0a\x09| testObject |\x0a\x09testObject := self jsObject.\x0a\x09self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString]) equals: nil.\x0a\x09self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.\x0a\x09self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString]) equals: 'hello 1'.\x0a\x09self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.",
+messageSends: ["jsObject", "assert:equals:", "at:ifPresent:", ",", "asString"],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxyTest);
+
+smalltalk.addMethod(
+"_testAtIfPresentIfAbsent",
+smalltalk.method({
+selector: "testAtIfPresentIfAbsent",
+category: 'tests',
+fn: function (){
+var self=this;
+var testObject;
+return smalltalk.withContext(function($ctx1) { 
testObject=_st(self)._jsObject();
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("abc",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"not present");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("e",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"hello nil");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("a",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"hello 1");
+_st(self)._assert_equals_(_st(testObject)._at_ifPresent_ifAbsent_("f",(function(x){
+return smalltalk.withContext(function($ctx2) {
return _st("hello ").__comma(_st(x)._asString());
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
return "not present";
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})})),"hello nil");
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfPresentIfAbsent",{testObject:testObject},smalltalk.JSObjectProxyTest)})},
+args: [],
+source: "testAtIfPresentIfAbsent\x0a\x09| testObject |\x0a\x09testObject := self jsObject.\x0a\x09self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'not present'.\x0a\x09self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.\x0a\x09self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello 1'.\x0a\x09self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.",
+messageSends: ["jsObject", "assert:equals:", "at:ifPresent:ifAbsent:", ",", "asString"],
+referencedClasses: []
+}),
+smalltalk.JSObjectProxyTest);
+
 smalltalk.addMethod(
 "_testDNU",
 smalltalk.method({

+ 19 - 0
st/Kernel-Objects.st

@@ -681,6 +681,25 @@ at: aSymbol ifAbsent: aBlock
 	>
 !
 
+at: aSymbol ifPresent: aBlock
+	"return the evaluation of aBlock with the value if the property is defined or return nil"
+	<
+		var obj = self['@jsObject'],
+			symbol = aSymbol._asString();
+		return symbol in obj ? aBlock(obj[symbol]) : nil;
+	>
+!
+
+at: aSymbol ifPresent: aBlock ifAbsent: anotherBlock
+	"return the evaluation of aBlock with the value if the property is defined
+	or return value of anotherBlock"
+	<
+		var obj = self['@jsObject'],
+			symbol = aSymbol._asString();
+		return symbol in obj ? aBlock(obj[symbol]) : anotherBlock();
+	>
+!
+
 at: aSymbol put: anObject
 	<self['@jsObject'][aSymbol._asString()] = anObject>
 !

+ 18 - 0
st/Kernel-Tests.st

@@ -985,6 +985,24 @@ testAtIfAbsent
 	self assert: (testObject at: 'f' ifAbsent: ['Property does not exist']) equals: nil.
 !
 
+testAtIfPresent
+	| testObject |
+	testObject := self jsObject.
+	self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString]) equals: nil.
+	self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.
+	self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString]) equals: 'hello 1'.
+	self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString]) equals: 'hello nil'.
+!
+
+testAtIfPresentIfAbsent
+	| testObject |
+	testObject := self jsObject.
+	self assert: (testObject at: 'abc' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'not present'.
+	self assert: (testObject at: 'e' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
+	self assert: (testObject at: 'a' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello 1'.
+	self assert: (testObject at: 'f' ifPresent: [:x|'hello ',x asString] ifAbsent: ['not present']) equals: 'hello nil'.
+!
+
 testDNU
 	self should: [self jsObject foo] raise: MessageNotUnderstood
 !