Skip to content

Commit

Permalink
feat: Add _intl operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Jul 14, 2022
1 parent 499b726 commit fbf4b14
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
@@ -0,0 +1,66 @@
/*
Copyright 2020-2022 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
// TODO: docs - arguments for all formatters
(arguments: {
on: date,
options?: object
locale?: string,
}): string
(arguments: [
on: date,
options?: object
locale?: string,
]): string
*/

import { runClass } from '@lowdefy/operators';

function createFormatter({ IntlClass }) {
const formatter = (on, options, locale) => {
return new IntlClass(locale, options).format(on);
};
return formatter;
}

const meta = {
// TODO: Is namedArgs order correct?
dateTimeFormat: { namedArgs: ['on', 'options', 'locale'], validTypes: ['array', 'object'] },
listFormat: { namedArgs: ['on', 'options', 'locale'], validTypes: ['array', 'object'] },
numberFormat: { namedArgs: ['on', 'options', 'locale'], validTypes: ['array', 'object'] },
relativeTimeFormat: { namedArgs: ['on', 'options', 'locale'], validTypes: ['array', 'object'] },
};

const functions = {
dateTimeFormat: createFormatter({ IntlClass: Intl.DateTimeFormat }),
listFormat: createFormatter({ IntlClass: Intl.ListFormat }),
numberFormat: createFormatter({ IntlClass: Intl.NumberFormat }),
relativeTimeFormat: createFormatter({ IntlClass: Intl.RelativeTimeFormat }),
};

function intl({ params, location, methodName }) {
return runClass({
functions,
location,
meta,
methodName,
operator: '_intl',
params,
});
}

export default intl;
@@ -0,0 +1,35 @@
/*
Copyright 2020-2022 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import intl from './intl.js';

describe('_intl.dateTimeFormat', () => {
const methodName = 'dateTimeFormat';
test('No options', () => {
expect(
intl({
params: {
on: new Date(1560414023345),
locale: 'de',
options: {
month: 'long',
},
},
location: 'locationId',
methodName,
})
).toEqual('Juni');
});
});
2 changes: 2 additions & 0 deletions packages/plugins/operators/operators-js/src/operatorsBuild.js
Expand Up @@ -26,6 +26,7 @@ import _gt from './operators/shared/gt.js';
import _gte from './operators/shared/gte.js';
import _if_none from './operators/shared/if_none.js';
import _if from './operators/shared/if.js';
import _intl from './operators/shared/intl.js';
import _json from './operators/shared/json.js';
import _log from './operators/shared/log.js';
import _lt from './operators/shared/lt.js';
Expand Down Expand Up @@ -68,6 +69,7 @@ export default {
_hash,
_if_none,
_if,
_intl,
_json,
_log,
_lt,
Expand Down
Expand Up @@ -26,6 +26,7 @@ export { default as _gt } from './operators/shared/gt.js';
export { default as _gte } from './operators/shared/gte.js';
export { default as _if_none } from './operators/shared/if_none.js';
export { default as _if } from './operators/shared/if.js';
export { default as _intl } from './operators/shared/intl.js';
export { default as _json } from './operators/shared/json.js';
export { default as _log } from './operators/shared/log.js';
export { default as _lt } from './operators/shared/lt.js';
Expand Down
Expand Up @@ -26,6 +26,7 @@ export { default as _gt } from './operators/shared/gt.js';
export { default as _gte } from './operators/shared/gte.js';
export { default as _if_none } from './operators/shared/if_none.js';
export { default as _if } from './operators/shared/if.js';
export { default as _intl } from './operators/shared/intl.js';
export { default as _json } from './operators/shared/json.js';
export { default as _log } from './operators/shared/log.js';
export { default as _lt } from './operators/shared/lt.js';
Expand Down

0 comments on commit fbf4b14

Please sign in to comment.